back to the example
This commit is contained in:
parent
b232b82ef5
commit
8e2e900a14
6
.gitignore
vendored
6
.gitignore
vendored
@ -13,11 +13,15 @@
|
||||
/*.c
|
||||
/*.h
|
||||
/.launches
|
||||
/Debug
|
||||
|
||||
# Node artifact files
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
# VS Code artifact files
|
||||
*.code-workspace
|
||||
|
||||
# Compiled Java class files
|
||||
*.class
|
||||
|
||||
@ -58,5 +62,3 @@ Thumbs.db
|
||||
*.flv
|
||||
*.mov
|
||||
*.wmv
|
||||
|
||||
/Debug/
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
#define IF_SEVERITY 0
|
||||
#include "./debug_if.h"
|
||||
|
||||
inline UART_Handle uartHandle;
|
||||
// UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);
|
||||
|
||||
#endif
|
||||
@ -58,9 +58,7 @@ enum
|
||||
};
|
||||
|
||||
#define IF_NAME "GEN"
|
||||
#ifndef IF_SEVERITY
|
||||
#define IF_SEVERITY E_INFO
|
||||
#endif
|
||||
|
||||
|
||||
#if (D_DEBUG_METHOD == D_UART_PRINT)
|
||||
@ -78,12 +76,12 @@ enum
|
||||
#endif
|
||||
|
||||
#define sl_printf(_severity_, ...) if(_severity_)>= D_SEVERITY) { PRINTF("[" IF_NAME "] "__VA_ARGS__); }
|
||||
#define LOG_FATAL( ...) if(E_FATAL >= IF_SEVERITY) { PRINTF("[" IF_NAME "::FATAL] "__VA_ARGS__); }
|
||||
#define LOG_ERROR( ...) if(E_ERROR >= IF_SEVERITY) { PRINTF("[" IF_NAME "::ERROR] "__VA_ARGS__); }
|
||||
#define LOG_WARNING( ...) if(E_WARNING >= IF_SEVERITY) { PRINTF("[" IF_NAME "::WARN] "__VA_ARGS__); }
|
||||
#define LOG_INFO( ...) if(E_INFO >= IF_SEVERITY) { PRINTF("[" IF_NAME "::INFO] "__VA_ARGS__); }
|
||||
#define LOG_DEBUG( ...) if(E_DEBUG >= IF_SEVERITY) { PRINTF("[" IF_NAME "::DEBUG] "__VA_ARGS__); }
|
||||
#define LOG_TRACE( ...) if(E_TRACE >= IF_SEVERITY) { PRINTF("[" IF_NAME "::TRACE] "__VA_ARGS__); }
|
||||
#define LOG_FATAL( ...) if(E_FATAL >= IF_SEVERITY) { PRINTF("[" IF_NAME "::FATAL] "__VA_ARGS__); }
|
||||
#define LOG_ERROR( ...) if(E_ERROR >= IF_SEVERITY) { PRINTF("[" IF_NAME "::ERROR] "__VA_ARGS__); }
|
||||
#define LOG_WARNING( ...) if(E_WARNING >= IF_SEVERITY) { PRINTF("[" IF_NAME "::WARN] "__VA_ARGS__); }
|
||||
#define LOG_INFO( ...) if(E_INFO >= IF_SEVERITY) { PRINTF("[" IF_NAME "::INFO] "__VA_ARGS__); }
|
||||
#define LOG_DEBUG( ...) if(E_DEBUG >= IF_SEVERITY) { PRINTF("[" IF_NAME "::DEBUG] "__VA_ARGS__); }
|
||||
#define LOG_TRACE( ...) if(E_TRACE >= IF_SEVERITY) { PRINTF("[" IF_NAME "::TRACE] "__VA_ARGS__); }
|
||||
|
||||
|
||||
#endif // DEBUG_IF_H
|
||||
@ -1,4 +0,0 @@
|
||||
|
||||
void * GSensorTask(void *args);
|
||||
void * MQTTTask(void *args);
|
||||
void * SocketTask(void *args);
|
||||
87
src/main.c
87
src/main.c
@ -1,87 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
#include <ti/sysbios/BIOS.h>
|
||||
#include <ti/drivers/GPIO.h>
|
||||
#include "ti_drivers_config.h"
|
||||
#include <ti/drivers/I2C.h>
|
||||
#include <ti/drivers/i2c/I2CCC32XX.h>
|
||||
|
||||
#include "header.h"
|
||||
//#include "debug/debug.h"
|
||||
//#include "network/network.h"
|
||||
|
||||
#define BIGOLTHREADSTACK 4096
|
||||
|
||||
void Hardware_init(){
|
||||
// int32_t ret;
|
||||
|
||||
Board_init();
|
||||
GPIO_init();
|
||||
I2C_init();
|
||||
|
||||
// ret = WifiInit();
|
||||
// if(ret < 0){
|
||||
// while(1);
|
||||
// }
|
||||
}
|
||||
|
||||
int main(void){
|
||||
|
||||
pthread_attr_t attrs;
|
||||
struct sched_param priParam;
|
||||
int retc;
|
||||
|
||||
Hardware_init(); // initialize hardware
|
||||
|
||||
/* Initialize the attributes structure with default values */
|
||||
pthread_attr_init(&attrs);
|
||||
|
||||
/* Set priority, detach state, and stack size attributes */
|
||||
priParam.sched_priority = 1;
|
||||
retc = pthread_attr_setschedparam(&attrs, &priParam);
|
||||
retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
|
||||
retc |= pthread_attr_setstacksize(&attrs, BIGOLTHREADSTACK);
|
||||
if (retc != 0) {
|
||||
// LOG_CRITICAL("main(): failed to set tread attributes");
|
||||
while(1);
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for G sensor =======================
|
||||
==============================================
|
||||
*/
|
||||
pthread_t GSensorThread;
|
||||
retc = pthread_create(&GSensorThread, &attrs, GSensorTask, NULL);
|
||||
if (retc != 0) {
|
||||
/* pthread_create() failed */
|
||||
// LOG_CRITICAL("main(): failed to create tread for G-sensor");
|
||||
while(1);
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for MQTT ===========================
|
||||
==============================================
|
||||
*/
|
||||
pthread_t MQTTThread;
|
||||
retc = pthread_create(&MQTTThread, &attrs, MQTTTask, NULL);
|
||||
if (retc != 0) {
|
||||
/* pthread_create() failed */
|
||||
// LOG_CRITICAL("main(): failed to create tread for MQTT");
|
||||
while(1);
|
||||
}
|
||||
|
||||
BIOS_start();
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* ======== dummyOutput ========
|
||||
* Dummy SysMin output function needed for benchmarks and size comparison
|
||||
* of FreeRTOS and TI-RTOS solutions.
|
||||
*/
|
||||
void dummyOutput(void)
|
||||
{
|
||||
}
|
||||
121
src/main_tirtos.c
Normal file
121
src/main_tirtos.c
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Texas Instruments Incorporated
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Texas Instruments Incorporated nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* ======== main_tirtos.c ========
|
||||
*/
|
||||
#include <stdint.h>
|
||||
|
||||
/* POSIX Header files */
|
||||
#include <pthread.h>
|
||||
|
||||
/* RTOS header files */
|
||||
#include <ti/sysbios/BIOS.h>
|
||||
|
||||
/* TI-RTOS Header files */
|
||||
#include <ti/drivers/GPIO.h>
|
||||
|
||||
/* TI-DRIVERS Header files */
|
||||
|
||||
#include "ti_drivers_config.h"
|
||||
|
||||
extern void * GSensorTask(void *arg0);
|
||||
extern void * MQTTTask(void *arg0);
|
||||
|
||||
/* Stack size in bytes */
|
||||
#define BIGOLTHREADSTACK 4096
|
||||
|
||||
/*
|
||||
* ======== main ========
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
pthread_attr_t attrs;
|
||||
struct sched_param priParam;
|
||||
int retc;
|
||||
|
||||
/* Call board init functions */
|
||||
Board_init();
|
||||
|
||||
/* Initialize the attributes structure with default values */
|
||||
pthread_attr_init(&attrs);
|
||||
|
||||
/* Set priority, detach state, and stack size attributes */
|
||||
priParam.sched_priority = 1;
|
||||
retc = pthread_attr_setschedparam(&attrs, &priParam);
|
||||
retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
|
||||
retc |= pthread_attr_setstacksize(&attrs, BIGOLTHREADSTACK);
|
||||
if (retc != 0) {
|
||||
// LOG_CRITICAL("main(): failed to set tread attributes");
|
||||
while(1);
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for G sensor =======================
|
||||
==============================================
|
||||
*/
|
||||
pthread_t GSensorThread;
|
||||
retc = pthread_create(&GSensorThread, &attrs, GSensorTask, NULL);
|
||||
if (retc != 0) {
|
||||
/* pthread_create() failed */
|
||||
// LOG_CRITICAL("main(): failed to create tread for G-sensor");
|
||||
while(1);
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for MQTT ===========================
|
||||
==============================================
|
||||
*/
|
||||
pthread_t MQTTThread;
|
||||
retc = pthread_create(&MQTTThread, &attrs, MQTTTask, NULL);
|
||||
if (retc != 0) {
|
||||
/* pthread_create() failed */
|
||||
// LOG_CRITICAL("main(): failed to create tread for MQTT");
|
||||
while(1);
|
||||
}
|
||||
|
||||
BIOS_start();
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* ======== dummyOutput ========
|
||||
* Dummy SysMin output function needed for benchmarks and size comparison
|
||||
* of FreeRTOS and TI-RTOS solutions.
|
||||
*/
|
||||
void dummyOutput(void)
|
||||
{
|
||||
}
|
||||
123
src/mqtt.c
123
src/mqtt.c
@ -1,123 +0,0 @@
|
||||
#include "mqtt/mqtt.h"
|
||||
|
||||
void MQTTTask(void* args){
|
||||
LOG_TRACE("MQTT task started.");
|
||||
mq_attr attr;
|
||||
MQTTClient_Handle mqttClientHandle;
|
||||
|
||||
SPI_init();
|
||||
|
||||
int32_t ret = ti_net_SlNet_initConfig();
|
||||
if(0 != ret)
|
||||
{
|
||||
LOG_ERROR("Failed to initialize SlNetSock\n\r");
|
||||
}
|
||||
|
||||
attr.mq_maxmsg = 10;
|
||||
attr.mq_msgsize = sizeof(struct msgQueue);
|
||||
appQueue = mq_open("appQueue", O_CREAT, 0, &attr);
|
||||
if(((int)appQueue) <= 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
// ret = WifiInit();
|
||||
// if(ret < 0){
|
||||
// while(1);
|
||||
// }
|
||||
|
||||
ret = MQTT_IF_Init(mqttInitParams);
|
||||
if(ret < 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* In case a persistent session is being used, subscribe is called before connect so that the module
|
||||
* is aware of the topic callbacks the user is using. This is important because if the broker is holding
|
||||
* messages for the client, after CONNACK the client may receive the messages before the module is aware
|
||||
* of the topic callbacks. The user may still call subscribe after connect but have to be aware of this.
|
||||
*/
|
||||
// ret = MQTT_IF_Subscribe(mqttClientHandle, "Broker/To/cc32xx", MQTT_QOS_2, BrokerCB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED1", MQTT_QOS_2, ToggleLED1CB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED2", MQTT_QOS_2, ToggleLED2CB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED3", MQTT_QOS_2, ToggleLED3CB);
|
||||
if(ret < 0){
|
||||
while(1);
|
||||
}else{
|
||||
LOG_INFO("Subscribed to all topics successfully\r\n");
|
||||
}
|
||||
|
||||
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
||||
if(mqttClientHandle < 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
// wait for CONNACK
|
||||
while(connected == 0);
|
||||
|
||||
|
||||
struct msgQueue queueElement;
|
||||
while(1){
|
||||
LOG_TRACE("Running MQTT loop.");
|
||||
mq_receive(appQueue, (char*)&queueElement, sizeof(struct msgQueue), NULL);
|
||||
|
||||
if(queueElement.event == APP_MQTT_PUBLISH){
|
||||
LOG_TRACE("APP_MQTT_PUBLISH\r\n");
|
||||
|
||||
MQTT_IF_Publish(mqttClientHandle,
|
||||
"cc32xx/ToggleLED1",
|
||||
"LED 1 toggle\r\n",
|
||||
strlen("LED 1 toggle\r\n"),
|
||||
MQTT_QOS_2);
|
||||
}
|
||||
else if(queueElement.event == APP_MQTT_CON_TOGGLE){
|
||||
|
||||
LOG_TRACE("APP_MQTT_CON_TOGGLE %d\r\n", connected);
|
||||
|
||||
if(connected){
|
||||
ret = MQTT_IF_Disconnect(mqttClientHandle);
|
||||
if(ret >= 0){
|
||||
connected = false;
|
||||
}
|
||||
}else{
|
||||
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
||||
if((int)mqttClientHandle >= 0){
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(queueElement.event == APP_MQTT_DEINIT){
|
||||
break;
|
||||
}
|
||||
else if(queueElement.event == APP_BTN_HANDLER){
|
||||
|
||||
struct msgQueue queueElement;
|
||||
|
||||
// ret = detectLongPress();
|
||||
// if(ret == 0){
|
||||
//
|
||||
//// LOG_TRACE("APP_BTN_HANDLER SHORT PRESS\r\n");
|
||||
// queueElement.event = APP_MQTT_CON_TOGGLE;
|
||||
// }
|
||||
// else{
|
||||
//
|
||||
//// LOG_TRACE("APP_BTN_HANDLER LONG PRESS\r\n");
|
||||
// queueElement.event = APP_MQTT_DEINIT;
|
||||
// }
|
||||
|
||||
ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
if(ret < 0){
|
||||
LOG_ERROR("msg queue send error %d", ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deinit = 1;
|
||||
if(connected){
|
||||
MQTT_IF_Disconnect(mqttClientHandle);
|
||||
}
|
||||
MQTT_IF_Deinit();
|
||||
|
||||
LOG_INFO("looping the MQTT functionality of the example for demonstration purposes only\r\n");
|
||||
// sleep(2);
|
||||
// goto MQTT_DEMO;
|
||||
}
|
||||
123
src/mqtt/mqtt.h
123
src/mqtt/mqtt.h
@ -1,123 +0,0 @@
|
||||
#include "mqtt_if.h"
|
||||
#include "../debug/debug.h"
|
||||
#include "../network/network.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
#include <ti/drivers/SPI.h>
|
||||
|
||||
#include <ti/net/mqtt/mqttclient.h>
|
||||
|
||||
// #define SL_TASKSTACKSIZE 2048
|
||||
// #define SPAWN_TASK_PRIORITY 9
|
||||
|
||||
// #define MQTT_MODULE_TASK_PRIORITY 2
|
||||
// #define MQTT_MODULE_TASK_STACK_SIZE 2048
|
||||
|
||||
#define MQTT_WILL_TOPIC "jesus_cc32xx_will_topic"
|
||||
#define MQTT_WILL_MSG "will_msg_works"
|
||||
#define MQTT_WILL_QOS MQTT_QOS_2
|
||||
#define MQTT_WILL_RETAIN false
|
||||
|
||||
#define MQTT_CLIENT_PASSWORD NULL
|
||||
#define MQTT_CLIENT_USERNAME NULL
|
||||
#define MQTT_CLIENT_KEEPALIVE 0
|
||||
#define MQTT_CLIENT_CLEAN_CONNECT true
|
||||
#define MQTT_CLIENT_MQTT_V3_1 true
|
||||
#define MQTT_CLIENT_BLOCKING_SEND true
|
||||
|
||||
#define MQTT_CONNECTION_FLAGS MQTTCLIENT_NETCONN_URL
|
||||
#define MQTT_CONNECTION_ADDRESS "mqtt.eclipse.org"
|
||||
#define MQTT_CONNECTION_PORT_NUMBER 1883
|
||||
|
||||
mqd_t appQueue;
|
||||
int connected;
|
||||
int deinit;
|
||||
|
||||
enum{
|
||||
APP_MQTT_PUBLISH,
|
||||
APP_MQTT_CON_TOGGLE,
|
||||
APP_MQTT_DEINIT,
|
||||
APP_BTN_HANDLER
|
||||
};
|
||||
|
||||
struct msgQueue
|
||||
{
|
||||
int event;
|
||||
char* payload;
|
||||
};
|
||||
|
||||
MQTT_IF_InitParams_t mqttInitParams =
|
||||
{
|
||||
2048, // stack size for mqtt module - default is 2048
|
||||
2 // thread priority for MQTT - default is 2
|
||||
};
|
||||
|
||||
MQTTClient_Will mqttWillParams =
|
||||
{
|
||||
MQTT_WILL_TOPIC, // will topic
|
||||
MQTT_WILL_MSG, // will message
|
||||
MQTT_WILL_QOS, // will QoS
|
||||
MQTT_WILL_RETAIN // retain flag
|
||||
};
|
||||
|
||||
MQTT_IF_ClientParams_t mqttClientParams =
|
||||
{
|
||||
ClientId, // client ID
|
||||
MQTT_CLIENT_USERNAME, // user name
|
||||
MQTT_CLIENT_PASSWORD, // password
|
||||
MQTT_CLIENT_KEEPALIVE, // keep-alive time
|
||||
MQTT_CLIENT_CLEAN_CONNECT, // clean connect flag
|
||||
MQTT_CLIENT_MQTT_V3_1, // true = 3.1, false = 3.1.1
|
||||
MQTT_CLIENT_BLOCKING_SEND, // blocking send flag
|
||||
&mqttWillParams // will parameters
|
||||
};
|
||||
|
||||
MQTTClient_ConnParams mqttConnParams =
|
||||
{
|
||||
MQTT_CONNECTION_FLAGS, // connection flags
|
||||
MQTT_CONNECTION_ADDRESS, // server address
|
||||
MQTT_CONNECTION_PORT_NUMBER, // port number of MQTT server
|
||||
0, // method for secure socket
|
||||
0, // cipher for secure socket
|
||||
0, // number of files for secure connection
|
||||
NULL // secure files
|
||||
};
|
||||
|
||||
void MQTT_EventCallback(int32_t event){
|
||||
struct msgQueue queueElement;
|
||||
switch(event){
|
||||
case MQTT_EVENT_CONNACK:
|
||||
deinit = 0;
|
||||
connected = true;
|
||||
LOG_INFO("MQTT_EVENT_CONNACK\r\n");
|
||||
break;
|
||||
case MQTT_EVENT_SUBACK:
|
||||
LOG_INFO("MQTT_EVENT_SUBACK\r\n");
|
||||
break;
|
||||
case MQTT_EVENT_PUBACK:
|
||||
LOG_INFO("MQTT_EVENT_PUBACK\r\n");
|
||||
break;
|
||||
case MQTT_EVENT_UNSUBACK:
|
||||
LOG_INFO("MQTT_EVENT_UNSUBACK\r\n");
|
||||
break;
|
||||
case MQTT_EVENT_CLIENT_DISCONNECT:
|
||||
connected = false;
|
||||
LOG_INFO("MQTT_EVENT_CLIENT_DISCONNECT\r\n");
|
||||
break;
|
||||
case MQTT_EVENT_SERVER_DISCONNECT:
|
||||
connected = false;
|
||||
LOG_INFO("MQTT_EVENT_SERVER_DISCONNECT\r\n");
|
||||
queueElement.event = APP_MQTT_CON_TOGGLE;
|
||||
int res = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
if(res < 0){
|
||||
LOG_ERROR("msg queue send error %d", res);
|
||||
}
|
||||
break;
|
||||
case MQTT_EVENT_DESTROY:
|
||||
LOG_INFO("MQTT_EVENT_DESTROY\r\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
695
src/mqtt_client_app.c
Normal file
695
src/mqtt_client_app.c
Normal file
@ -0,0 +1,695 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Texas Instruments Incorporated
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Texas Instruments Incorporated nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
Application Name - MQTT Client
|
||||
Application Overview - The device is running a MQTT client which is
|
||||
connected to the online broker. Three LEDs on the
|
||||
device can be controlled from a web client by
|
||||
publishing msg on appropriate topics. Similarly,
|
||||
message can be published on pre-configured topics
|
||||
by pressing the switch buttons on the device.
|
||||
|
||||
Application Details - Refer to 'MQTT Client' README.html
|
||||
|
||||
*****************************************************************************/
|
||||
#include "mqtt_if.h"
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
#include <ti/drivers/net/wifi/simplelink.h>
|
||||
#include <ti/drivers/net/wifi/slnetifwifi.h>
|
||||
|
||||
#include <ti/drivers/SPI.h>
|
||||
// #include <ti/drivers/GPIO.h>
|
||||
// #include <ti/drivers/Timer.h>
|
||||
|
||||
#include <ti/net/mqtt/mqttclient.h>
|
||||
|
||||
#include "network_if.h"
|
||||
#include "uart_term.h"
|
||||
#include "mqtt_if.h"
|
||||
#include "debug_if.h"
|
||||
|
||||
#include "ti_drivers_config.h"
|
||||
|
||||
extern int32_t ti_net_SlNet_initConfig();
|
||||
|
||||
#define APPLICATION_NAME "Eindopdracht"
|
||||
#define APPLICATION_VERSION "0.1.0"
|
||||
|
||||
#define SL_TASKSTACKSIZE 2048
|
||||
#define SPAWN_TASK_PRIORITY 9
|
||||
|
||||
// un-comment this if you want to connect to an MQTT broker securely
|
||||
//#define MQTT_SECURE_CLIENT
|
||||
|
||||
#define MQTT_MODULE_TASK_PRIORITY 2
|
||||
#define MQTT_MODULE_TASK_STACK_SIZE 2048
|
||||
|
||||
#define MQTT_WILL_TOPIC "jesus_cc32xx_will_topic"
|
||||
#define MQTT_WILL_MSG "will_msg_works"
|
||||
#define MQTT_WILL_QOS MQTT_QOS_2
|
||||
#define MQTT_WILL_RETAIN false
|
||||
|
||||
#define MQTT_CLIENT_PASSWORD NULL
|
||||
#define MQTT_CLIENT_USERNAME NULL
|
||||
#define MQTT_CLIENT_KEEPALIVE 0
|
||||
#define MQTT_CLIENT_CLEAN_CONNECT true
|
||||
#define MQTT_CLIENT_MQTT_V3_1 true
|
||||
#define MQTT_CLIENT_BLOCKING_SEND true
|
||||
|
||||
#ifndef MQTT_SECURE_CLIENT
|
||||
#define MQTT_CONNECTION_FLAGS MQTTCLIENT_NETCONN_URL
|
||||
#define MQTT_CONNECTION_ADDRESS "mqtt.eclipse.org"
|
||||
#define MQTT_CONNECTION_PORT_NUMBER 1883
|
||||
#else
|
||||
#define MQTT_CONNECTION_FLAGS MQTTCLIENT_NETCONN_IP4 | MQTTCLIENT_NETCONN_SEC
|
||||
#define MQTT_CONNECTION_ADDRESS "192.168.178.67"
|
||||
#define MQTT_CONNECTION_PORT_NUMBER 8883
|
||||
#endif
|
||||
|
||||
mqd_t appQueue;
|
||||
int connected;
|
||||
int deinit;
|
||||
int longPress = 0;
|
||||
|
||||
/* Client ID */
|
||||
/* If ClientId isn't set, the MAC address of the device will be copied into */
|
||||
/* the ClientID parameter. */
|
||||
char ClientId[13] = {'\0'};
|
||||
|
||||
enum{
|
||||
APP_MQTT_PUBLISH,
|
||||
APP_MQTT_CON_TOGGLE,
|
||||
APP_MQTT_DEINIT,
|
||||
APP_BTN_HANDLER
|
||||
};
|
||||
|
||||
struct msgQueue
|
||||
{
|
||||
int event;
|
||||
char* payload;
|
||||
};
|
||||
|
||||
MQTT_IF_InitParams_t mqttInitParams =
|
||||
{
|
||||
MQTT_MODULE_TASK_STACK_SIZE, // stack size for mqtt module - default is 2048
|
||||
MQTT_MODULE_TASK_PRIORITY // thread priority for MQTT - default is 2
|
||||
};
|
||||
|
||||
MQTTClient_Will mqttWillParams =
|
||||
{
|
||||
MQTT_WILL_TOPIC, // will topic
|
||||
MQTT_WILL_MSG, // will message
|
||||
MQTT_WILL_QOS, // will QoS
|
||||
MQTT_WILL_RETAIN // retain flag
|
||||
};
|
||||
|
||||
MQTT_IF_ClientParams_t mqttClientParams =
|
||||
{
|
||||
ClientId, // client ID
|
||||
MQTT_CLIENT_USERNAME, // user name
|
||||
MQTT_CLIENT_PASSWORD, // password
|
||||
MQTT_CLIENT_KEEPALIVE, // keep-alive time
|
||||
MQTT_CLIENT_CLEAN_CONNECT, // clean connect flag
|
||||
MQTT_CLIENT_MQTT_V3_1, // true = 3.1, false = 3.1.1
|
||||
MQTT_CLIENT_BLOCKING_SEND, // blocking send flag
|
||||
&mqttWillParams // will parameters
|
||||
};
|
||||
|
||||
#ifndef MQTT_SECURE_CLIENT
|
||||
MQTTClient_ConnParams mqttConnParams =
|
||||
{
|
||||
MQTT_CONNECTION_FLAGS, // connection flags
|
||||
MQTT_CONNECTION_ADDRESS, // server address
|
||||
MQTT_CONNECTION_PORT_NUMBER, // port number of MQTT server
|
||||
0, // method for secure socket
|
||||
0, // cipher for secure socket
|
||||
0, // number of files for secure connection
|
||||
NULL // secure files
|
||||
};
|
||||
#else
|
||||
/*
|
||||
* In order to connect to an MQTT broker securely, the MQTTCLIENT_NETCONN_SEC flag,
|
||||
* method for secure socket, cipher, secure files, number of secure files must be set
|
||||
* and the certificates must be programmed to the file system.
|
||||
*
|
||||
* The first parameter is a bit mask which configures the server address type and security mode.
|
||||
* Server address type: IPv4, IPv6 and URL must be declared with the corresponding flag.
|
||||
* All flags can be found in mqttclient.h.
|
||||
*
|
||||
* The flag MQTTCLIENT_NETCONN_SEC enables the security (TLS) which includes domain name
|
||||
* verification and certificate catalog verification. Those verifications can be skipped by
|
||||
* adding to the bit mask: MQTTCLIENT_NETCONN_SKIP_DOMAIN_NAME_VERIFICATION and
|
||||
* MQTTCLIENT_NETCONN_SKIP_CERTIFICATE_CATALOG_VERIFICATION.
|
||||
*
|
||||
* Note: The domain name verification requires URL Server address type otherwise, this
|
||||
* verification will be disabled.
|
||||
*
|
||||
* Secure clients require time configuration in order to verify the server certificate validity (date)
|
||||
*/
|
||||
|
||||
/* Day of month (DD format) range 1-31 */
|
||||
#define DAY 1
|
||||
/* Month (MM format) in the range of 1-12 */
|
||||
#define MONTH 5
|
||||
/* Year (YYYY format) */
|
||||
#define YEAR 2020
|
||||
/* Hours in the range of 0-23 */
|
||||
#define HOUR 4
|
||||
/* Minutes in the range of 0-59 */
|
||||
#define MINUTES 00
|
||||
/* Seconds in the range of 0-59 */
|
||||
#define SEC 00
|
||||
|
||||
char *MQTTClient_secureFiles[1] = {"ca-cert.pem"};
|
||||
|
||||
MQTTClient_ConnParams mqttConnParams =
|
||||
{
|
||||
MQTT_CONNECTION_FLAGS, // connection flags
|
||||
MQTT_CONNECTION_ADDRESS, // server address
|
||||
MQTT_CONNECTION_PORT_NUMBER, // port number of MQTT server
|
||||
SLNETSOCK_SEC_METHOD_SSLv3_TLSV1_2, // method for secure socket
|
||||
SLNETSOCK_SEC_CIPHER_FULL_LIST, // cipher for secure socket
|
||||
1, // number of files for secure connection
|
||||
MQTTClient_secureFiles // secure files
|
||||
};
|
||||
|
||||
void setTime(){
|
||||
|
||||
SlDateTime_t dateTime = {0};
|
||||
dateTime.tm_day = (uint32_t)DAY;
|
||||
dateTime.tm_mon = (uint32_t)MONTH;
|
||||
dateTime.tm_year = (uint32_t)YEAR;
|
||||
dateTime.tm_hour = (uint32_t)HOUR;
|
||||
dateTime.tm_min = (uint32_t)MINUTES;
|
||||
dateTime.tm_sec = (uint32_t)SEC;
|
||||
sl_DeviceSet(SL_DEVICE_GENERAL, SL_DEVICE_GENERAL_DATE_TIME,
|
||||
sizeof(SlDateTime_t), (uint8_t *)(&dateTime));
|
||||
}
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//!
|
||||
//! Set the ClientId with its own mac address
|
||||
//! This routine converts the mac address which is given
|
||||
//! by an integer type variable in hexadecimal base to ASCII
|
||||
//! representation, and copies it into the ClientId parameter.
|
||||
//!
|
||||
//! \param macAddress - Points to string Hex.
|
||||
//!
|
||||
//! \return void.
|
||||
//!
|
||||
//*****************************************************************************
|
||||
int32_t SetClientIdNamefromMacAddress()
|
||||
{
|
||||
int32_t ret = 0;
|
||||
uint8_t Client_Mac_Name[2];
|
||||
uint8_t Index;
|
||||
uint16_t macAddressLen = SL_MAC_ADDR_LEN;
|
||||
uint8_t macAddress[SL_MAC_ADDR_LEN];
|
||||
|
||||
/*Get the device Mac address */
|
||||
ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen,
|
||||
&macAddress[0]);
|
||||
|
||||
/*When ClientID isn't set, use the mac address as ClientID */
|
||||
if(ClientId[0] == '\0')
|
||||
{
|
||||
/*6 bytes is the length of the mac address */
|
||||
for(Index = 0; Index < SL_MAC_ADDR_LEN; Index++)
|
||||
{
|
||||
/*Each mac address byte contains two hexadecimal characters */
|
||||
/*Copy the 4 MSB - the most significant character */
|
||||
Client_Mac_Name[0] = (macAddress[Index] >> 4) & 0xf;
|
||||
/*Copy the 4 LSB - the least significant character */
|
||||
Client_Mac_Name[1] = macAddress[Index] & 0xf;
|
||||
|
||||
if(Client_Mac_Name[0] > 9)
|
||||
{
|
||||
/*Converts and copies from number that is greater than 9 in */
|
||||
/*hexadecimal representation (a to f) into ascii character */
|
||||
ClientId[2 * Index] = Client_Mac_Name[0] + 'a' - 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*Converts and copies from number 0 - 9 in hexadecimal */
|
||||
/*representation into ascii character */
|
||||
ClientId[2 * Index] = Client_Mac_Name[0] + '0';
|
||||
}
|
||||
if(Client_Mac_Name[1] > 9)
|
||||
{
|
||||
/*Converts and copies from number that is greater than 9 in */
|
||||
/*hexadecimal representation (a to f) into ascii character */
|
||||
ClientId[2 * Index + 1] = Client_Mac_Name[1] + 'a' - 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*Converts and copies from number 0 - 9 in hexadecimal */
|
||||
/*representation into ascii character */
|
||||
ClientId[2 * Index + 1] = Client_Mac_Name[1] + '0';
|
||||
}
|
||||
}
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
// void timerCallback(Timer_Handle myHandle)
|
||||
// {
|
||||
// longPress = 1;
|
||||
// }
|
||||
|
||||
// this timer callback toggles the LED once per second until the device connects to an AP
|
||||
// void timerLEDCallback(Timer_Handle myHandle)
|
||||
// {
|
||||
// GPIO_toggle(CONFIG_GPIO_LED_0);
|
||||
// }
|
||||
|
||||
// void pushButtonPublishHandler(uint_least8_t index)
|
||||
// {
|
||||
// int ret;
|
||||
// struct msgQueue queueElement;
|
||||
|
||||
// GPIO_disableInt(CONFIG_GPIO_BUTTON_0);
|
||||
|
||||
// queueElement.event = APP_MQTT_PUBLISH;
|
||||
// ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
// if(ret < 0){
|
||||
// LOG_ERROR("msg queue send error %d", ret);
|
||||
// }
|
||||
// }
|
||||
|
||||
// void pushButtonConnectionHandler(uint_least8_t index)
|
||||
// {
|
||||
// int ret;
|
||||
// struct msgQueue queueElement;
|
||||
|
||||
// GPIO_disableInt(CONFIG_GPIO_BUTTON_1);
|
||||
|
||||
// ret = Timer_start(timer0);
|
||||
// if(ret < 0){
|
||||
// LOG_ERROR("failed to start the timer\r\n");
|
||||
// }
|
||||
|
||||
// queueElement.event = APP_BTN_HANDLER;
|
||||
|
||||
// ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
// if(ret < 0){
|
||||
// LOG_ERROR("msg queue send error %d", ret);
|
||||
// }
|
||||
// }
|
||||
|
||||
// int detectLongPress(){
|
||||
|
||||
// int buttonPressed;
|
||||
|
||||
// do{
|
||||
// buttonPressed = GPIO_read(CONFIG_GPIO_BUTTON_1);
|
||||
// }while(buttonPressed && !longPress);
|
||||
|
||||
// // disabling the timer in case the callback has not yet triggered to avoid updating longPress
|
||||
// Timer_stop(timer0);
|
||||
|
||||
// if(longPress == 1){
|
||||
// longPress = 0;
|
||||
// return 1;
|
||||
// }
|
||||
// else{
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
void MQTT_EventCallback(int32_t event){
|
||||
|
||||
struct msgQueue queueElement;
|
||||
|
||||
switch(event){
|
||||
|
||||
case MQTT_EVENT_CONNACK:
|
||||
{
|
||||
deinit = 0;
|
||||
connected = 1;
|
||||
LOG_INFO("MQTT_EVENT_CONNACK\r\n");
|
||||
// GPIO_clearInt(CONFIG_GPIO_BUTTON_1);
|
||||
// GPIO_enableInt(CONFIG_GPIO_BUTTON_1);
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_SUBACK:
|
||||
{
|
||||
LOG_INFO("MQTT_EVENT_SUBACK\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_PUBACK:
|
||||
{
|
||||
LOG_INFO("MQTT_EVENT_PUBACK\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_UNSUBACK:
|
||||
{
|
||||
LOG_INFO("MQTT_EVENT_UNSUBACK\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_CLIENT_DISCONNECT:
|
||||
{
|
||||
connected = 0;
|
||||
LOG_INFO("MQTT_EVENT_CLIENT_DISCONNECT\r\n");
|
||||
if(deinit == 0){
|
||||
// GPIO_clearInt(CONFIG_GPIO_BUTTON_1);
|
||||
// GPIO_enableInt(CONFIG_GPIO_BUTTON_1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_SERVER_DISCONNECT:
|
||||
{
|
||||
connected = 0;
|
||||
|
||||
LOG_INFO("MQTT_EVENT_SERVER_DISCONNECT\r\n");
|
||||
|
||||
queueElement.event = APP_MQTT_CON_TOGGLE;
|
||||
int res = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
if(res < 0){
|
||||
LOG_ERROR("msg queue send error %d", res);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_DESTROY:
|
||||
{
|
||||
LOG_INFO("MQTT_EVENT_DESTROY\r\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Subscribe topic callbacks
|
||||
* Topic and payload data is deleted after topic callbacks return.
|
||||
* User must copy the topic or payload data if it needs to be saved.
|
||||
*/
|
||||
void BrokerCB(char* topic, char* payload){
|
||||
LOG_INFO("TOPIC: %s \tPAYLOAD: %s\r\n", topic, payload);
|
||||
}
|
||||
|
||||
// void ToggleLED1CB(char* topic, char* payload){
|
||||
// GPIO_toggle(CONFIG_GPIO_LED_0);
|
||||
// LOG_INFO("TOPIC: %s \tPAYLOAD: %s\r\n", topic, payload);
|
||||
// }
|
||||
|
||||
// void ToggleLED2CB(char* topic, char* payload){
|
||||
// GPIO_toggle(CONFIG_GPIO_LED_1);
|
||||
// LOG_INFO("TOPIC: %s \tPAYLOAD: %s\r\n", topic, payload);
|
||||
// }
|
||||
|
||||
// void ToggleLED3CB(char* topic, char* payload){
|
||||
// GPIO_toggle(CONFIG_GPIO_LED_2);
|
||||
// LOG_INFO("TOPIC: %s \tPAYLOAD: %s\r\n", topic, payload);
|
||||
// }
|
||||
|
||||
int32_t DisplayAppBanner(char* appName, char* appVersion){
|
||||
|
||||
int32_t ret = 0;
|
||||
uint8_t macAddress[SL_MAC_ADDR_LEN];
|
||||
uint16_t macAddressLen = SL_MAC_ADDR_LEN;
|
||||
uint16_t ConfigSize = 0;
|
||||
uint8_t ConfigOpt = SL_DEVICE_GENERAL_VERSION;
|
||||
SlDeviceVersion_t ver = {0};
|
||||
|
||||
ConfigSize = sizeof(SlDeviceVersion_t);
|
||||
|
||||
// get the device version info and MAC address
|
||||
ret = sl_DeviceGet(SL_DEVICE_GENERAL, &ConfigOpt, &ConfigSize, (uint8_t*)(&ver));
|
||||
ret |= (int32_t)sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen, &macAddress[0]);
|
||||
|
||||
UART_PRINT("\n\r\t============================================\n\r");
|
||||
UART_PRINT("\t %s V%s",appName, appVersion);
|
||||
UART_PRINT("\n\r\t============================================\n\r\n\r");
|
||||
|
||||
UART_PRINT("\t CHIP: 0x%x\n\r",ver.ChipId);
|
||||
UART_PRINT("\t MAC: %d.%d.%d.%d\n\r",ver.FwVersion[0],ver.FwVersion[1],
|
||||
ver.FwVersion[2],
|
||||
ver.FwVersion[3]);
|
||||
UART_PRINT("\t PHY: %d.%d.%d.%d\n\r",ver.PhyVersion[0],ver.PhyVersion[1],
|
||||
ver.PhyVersion[2],
|
||||
ver.PhyVersion[3]);
|
||||
UART_PRINT("\t NWP: %d.%d.%d.%d\n\r",ver.NwpVersion[0],ver.NwpVersion[1],
|
||||
ver.NwpVersion[2],
|
||||
ver.NwpVersion[3]);
|
||||
UART_PRINT("\t ROM: %d\n\r",ver.RomVersion);
|
||||
UART_PRINT("\t HOST: %s\n\r", SL_DRIVER_VERSION);
|
||||
UART_PRINT("\t MAC address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", macAddress[0],
|
||||
macAddress[1], macAddress[2], macAddress[3], macAddress[4],
|
||||
macAddress[5]);
|
||||
UART_PRINT("\n\r\t============================================\n\r");
|
||||
|
||||
return(ret);
|
||||
}
|
||||
|
||||
int WifiInit(){
|
||||
|
||||
int32_t ret;
|
||||
SlWlanSecParams_t security_params;
|
||||
pthread_t spawn_thread = (pthread_t) NULL;
|
||||
pthread_attr_t pattrs_spawn;
|
||||
struct sched_param pri_param;
|
||||
|
||||
pthread_attr_init(&pattrs_spawn);
|
||||
pri_param.sched_priority = SPAWN_TASK_PRIORITY;
|
||||
ret = pthread_attr_setschedparam(&pattrs_spawn, &pri_param);
|
||||
ret |= pthread_attr_setstacksize(&pattrs_spawn, SL_TASKSTACKSIZE);
|
||||
ret |= pthread_attr_setdetachstate(&pattrs_spawn, PTHREAD_CREATE_DETACHED);
|
||||
ret = pthread_create(&spawn_thread, &pattrs_spawn, sl_Task, NULL);
|
||||
if(ret != 0){
|
||||
LOG_ERROR("could not create simplelink task\n\r");
|
||||
while(1);
|
||||
}
|
||||
|
||||
Network_IF_ResetMCUStateMachine();
|
||||
|
||||
Network_IF_DeInitDriver();
|
||||
|
||||
ret = Network_IF_InitDriver(ROLE_STA);
|
||||
if(ret < 0){
|
||||
LOG_ERROR("Failed to start SimpleLink Device\n\r");
|
||||
while(1);
|
||||
}
|
||||
|
||||
DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION);
|
||||
|
||||
SetClientIdNamefromMacAddress();
|
||||
|
||||
security_params.Key = (signed char*)SECURITY_KEY;
|
||||
security_params.KeyLen = strlen(SECURITY_KEY);
|
||||
security_params.Type = SECURITY_TYPE;
|
||||
|
||||
ret = Network_IF_ConnectAP(SSID_NAME, security_params);
|
||||
if(ret < 0){
|
||||
LOG_ERROR("Connection to an AP failed\n\r");
|
||||
}
|
||||
else{
|
||||
|
||||
SlWlanSecParams_t securityParams;
|
||||
|
||||
securityParams.Type = SECURITY_TYPE;
|
||||
securityParams.Key = (signed char*)SECURITY_KEY;
|
||||
securityParams.KeyLen = strlen((const char *)securityParams.Key);
|
||||
|
||||
ret = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &securityParams, NULL, 7, 0);
|
||||
if(ret < 0){
|
||||
LOG_ERROR("failed to add profile %s\r\n", SSID_NAME);
|
||||
}
|
||||
else{
|
||||
LOG_INFO("profile added %s\r\n", SSID_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void MQTTTask(void * args){
|
||||
|
||||
int32_t ret;
|
||||
mq_attr attr;
|
||||
UART_Handle uartHandle;
|
||||
struct msgQueue queueElement;
|
||||
MQTTClient_Handle mqttClientHandle;
|
||||
|
||||
uartHandle = InitTerm();
|
||||
UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);
|
||||
|
||||
SPI_init();
|
||||
|
||||
ret = ti_net_SlNet_initConfig();
|
||||
if(0 != ret)
|
||||
{
|
||||
LOG_ERROR("Failed to initialize SlNetSock\n\r");
|
||||
}
|
||||
|
||||
attr.mq_maxmsg = 10;
|
||||
attr.mq_msgsize = sizeof(struct msgQueue);
|
||||
appQueue = mq_open("appQueue", O_CREAT, 0, &attr);
|
||||
if(((int)appQueue) <= 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
ret = WifiInit();
|
||||
if(ret < 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
MQTT_DEMO:
|
||||
|
||||
ret = MQTT_IF_Init(mqttInitParams);
|
||||
if(ret < 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
#ifdef MQTT_SECURE_CLIENT
|
||||
setTime();
|
||||
#endif
|
||||
|
||||
/*
|
||||
* In case a persistent session is being used, subscribe is called before connect so that the module
|
||||
* is aware of the topic callbacks the user is using. This is important because if the broker is holding
|
||||
* messages for the client, after CONNACK the client may receive the messages before the module is aware
|
||||
* of the topic callbacks. The user may still call subscribe after connect but have to be aware of this.
|
||||
*/
|
||||
ret = 0;
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "Broker/To/cc32xx", MQTT_QOS_2, BrokerCB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED1", MQTT_QOS_2, ToggleLED1CB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED2", MQTT_QOS_2, ToggleLED2CB);
|
||||
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED3", MQTT_QOS_2, ToggleLED3CB);
|
||||
ret |= MQTT_IF_Subscribe(mqttClientHandle, "ems20/handshake/0964590/reply", MQTT_QOS_2, BrokerCB);
|
||||
ret |= MQTT_IF_Subscribe(mqttClientHandle, "ems20/handshake/0971472/request", MQTT_QOS_2, BrokerCB);
|
||||
if(ret < 0){
|
||||
while(1);
|
||||
}
|
||||
else{
|
||||
LOG_INFO("Subscribed to all topics successfully\r\n");
|
||||
}
|
||||
|
||||
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
||||
if(mqttClientHandle < 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
// wait for CONNACK
|
||||
while(connected == 0);
|
||||
|
||||
// GPIO_enableInt(CONFIG_GPIO_BUTTON_0);
|
||||
|
||||
while(1){
|
||||
|
||||
mq_receive(appQueue, (char*)&queueElement, sizeof(struct msgQueue), NULL);
|
||||
|
||||
if(queueElement.event == APP_MQTT_PUBLISH){
|
||||
|
||||
LOG_TRACE("APP_MQTT_PUBLISH\r\n");
|
||||
|
||||
MQTT_IF_Publish(mqttClientHandle,
|
||||
"cc32xx/ToggleLED1",
|
||||
"LED 1 toggle\r\n",
|
||||
strlen("LED 1 toggle\r\n"),
|
||||
MQTT_QOS_2);
|
||||
|
||||
}
|
||||
else if(queueElement.event == APP_MQTT_CON_TOGGLE){
|
||||
|
||||
LOG_TRACE("APP_MQTT_CON_TOGGLE %d\r\n", connected);
|
||||
|
||||
|
||||
if(connected){
|
||||
ret = MQTT_IF_Disconnect(mqttClientHandle);
|
||||
if(ret >= 0){
|
||||
connected = 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
||||
if((int)mqttClientHandle >= 0){
|
||||
connected = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(queueElement.event == APP_MQTT_DEINIT){
|
||||
break;
|
||||
}
|
||||
else if(queueElement.event == APP_BTN_HANDLER){
|
||||
LOG_TRACE("APP_BTN_HANDLER: does nothing\r\n");
|
||||
|
||||
// struct msgQueue queueElement;
|
||||
|
||||
// ret = detectLongPress();
|
||||
// if(ret == 0){
|
||||
|
||||
// LOG_TRACE("APP_BTN_HANDLER SHORT PRESS\r\n");
|
||||
// queueElement.event = APP_MQTT_CON_TOGGLE;
|
||||
// }
|
||||
// else{
|
||||
|
||||
// LOG_TRACE("APP_BTN_HANDLER LONG PRESS\r\n");
|
||||
// queueElement.event = APP_MQTT_DEINIT;
|
||||
// }
|
||||
|
||||
// ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
||||
// if(ret < 0){
|
||||
// LOG_ERROR("msg queue send error %d", ret);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
deinit = 1;
|
||||
if(connected){
|
||||
MQTT_IF_Disconnect(mqttClientHandle);
|
||||
}
|
||||
MQTT_IF_Deinit();
|
||||
|
||||
LOG_INFO("looping the MQTT functionality of the example for demonstration purposes only\r\n");
|
||||
sleep(2);
|
||||
goto MQTT_DEMO;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//*****************************************************************************
|
||||
@ -12,7 +12,7 @@
|
||||
#include <pthread.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
#include "../debug/debug.h"
|
||||
#include "debug_if.h"
|
||||
|
||||
|
||||
enum{
|
||||
@ -1,126 +0,0 @@
|
||||
|
||||
|
||||
#ifndef __NETWORK__H__
|
||||
#define __NETWORK__H__
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "network_if.h"
|
||||
|
||||
extern char ClientId[13] = {'\0'};
|
||||
|
||||
int32_t SetClientIdNamefromMacAddress()
|
||||
{
|
||||
int32_t ret = 0;
|
||||
uint8_t Client_Mac_Name[2];
|
||||
uint8_t Index;
|
||||
uint16_t macAddressLen = SL_MAC_ADDR_LEN;
|
||||
uint8_t macAddress[SL_MAC_ADDR_LEN];
|
||||
|
||||
/*Get the device Mac address */
|
||||
ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen,
|
||||
&macAddress[0]);
|
||||
|
||||
/*When ClientID isn't set, use the mac address as ClientID */
|
||||
if(ClientId[0] == '\0')
|
||||
{
|
||||
/*6 bytes is the length of the mac address */
|
||||
for(Index = 0; Index < SL_MAC_ADDR_LEN; Index++)
|
||||
{
|
||||
/*Each mac address byte contains two hexadecimal characters */
|
||||
/*Copy the 4 MSB - the most significant character */
|
||||
Client_Mac_Name[0] = (macAddress[Index] >> 4) & 0xf;
|
||||
/*Copy the 4 LSB - the least significant character */
|
||||
Client_Mac_Name[1] = macAddress[Index] & 0xf;
|
||||
|
||||
if(Client_Mac_Name[0] > 9)
|
||||
{
|
||||
/*Converts and copies from number that is greater than 9 in */
|
||||
/*hexadecimal representation (a to f) into ascii character */
|
||||
ClientId[2 * Index] = Client_Mac_Name[0] + 'a' - 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*Converts and copies from number 0 - 9 in hexadecimal */
|
||||
/*representation into ascii character */
|
||||
ClientId[2 * Index] = Client_Mac_Name[0] + '0';
|
||||
}
|
||||
if(Client_Mac_Name[1] > 9)
|
||||
{
|
||||
/*Converts and copies from number that is greater than 9 in */
|
||||
/*hexadecimal representation (a to f) into ascii character */
|
||||
ClientId[2 * Index + 1] = Client_Mac_Name[1] + 'a' - 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*Converts and copies from number 0 - 9 in hexadecimal */
|
||||
/*representation into ascii character */
|
||||
ClientId[2 * Index + 1] = Client_Mac_Name[1] + '0';
|
||||
}
|
||||
}
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
int WifiInit(){
|
||||
|
||||
int32_t ret;
|
||||
SlWlanSecParams_t security_params;
|
||||
pthread_t spawn_thread = (pthread_t) NULL;
|
||||
pthread_attr_t pattrs_spawn;
|
||||
struct sched_param pri_param;
|
||||
|
||||
pthread_attr_init(&pattrs_spawn);
|
||||
pri_param.sched_priority = 9;
|
||||
ret = pthread_attr_setschedparam(&pattrs_spawn, &pri_param);
|
||||
ret |= pthread_attr_setstacksize(&pattrs_spawn, 2048);
|
||||
ret |= pthread_attr_setdetachstate(&pattrs_spawn, PTHREAD_CREATE_DETACHED);
|
||||
ret = pthread_create(&spawn_thread, &pattrs_spawn, sl_Task, NULL);
|
||||
if(ret != 0){
|
||||
// LOG_ERROR("could not create simplelink task\n\r");
|
||||
while(1);
|
||||
}
|
||||
|
||||
Network_IF_ResetMCUStateMachine();
|
||||
|
||||
Network_IF_DeInitDriver();
|
||||
|
||||
ret = Network_IF_InitDriver(ROLE_STA);
|
||||
if(ret < 0){
|
||||
// LOG_ERROR("Failed to start SimpleLink Device\n\r");
|
||||
while(1);
|
||||
}
|
||||
|
||||
// DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION);
|
||||
|
||||
SetClientIdNamefromMacAddress();
|
||||
|
||||
security_params.Key = (signed char*)SECURITY_KEY;
|
||||
security_params.KeyLen = strlen(SECURITY_KEY);
|
||||
security_params.Type = SECURITY_TYPE;
|
||||
|
||||
ret = Network_IF_ConnectAP(SSID_NAME, security_params);
|
||||
if(ret < 0){
|
||||
// LOG_ERROR("Connection to an AP failed\n\r");
|
||||
}
|
||||
else{
|
||||
|
||||
SlWlanSecParams_t securityParams;
|
||||
|
||||
securityParams.Type = SECURITY_TYPE;
|
||||
securityParams.Key = (signed char*)SECURITY_KEY;
|
||||
securityParams.KeyLen = strlen((const char *)securityParams.Key);
|
||||
|
||||
ret = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &securityParams, NULL, 7, 0);
|
||||
if(ret < 0){
|
||||
// LOG_ERROR("failed to add profile %s\r\n", SSID_NAME);
|
||||
}
|
||||
else{
|
||||
// LOG_INFO("profile added %s\r\n", SSID_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -56,7 +56,7 @@
|
||||
|
||||
/* Common interface includes */
|
||||
#include "network_if.h"
|
||||
#include "../debug/uart_term.h"
|
||||
#include "uart_term.h"
|
||||
|
||||
//*****************************************************************************
|
||||
// LOCAL DEFINES
|
||||
@ -535,7 +535,7 @@ long Network_IF_DeInitDriver(void)
|
||||
{
|
||||
long lRetVal = -1;
|
||||
|
||||
// UART_PRINT("SL Disconnect...\n\r");
|
||||
UART_PRINT("SL Disconnect...\n\r");
|
||||
|
||||
/* Disconnect from the AP */
|
||||
lRetVal = Network_IF_DisconnectFromAP();
|
||||
@ -54,20 +54,7 @@ extern "C"
|
||||
/* Simplelink includes */
|
||||
#include <ti/drivers/net/wifi/simplelink.h>
|
||||
|
||||
/* Values for below macros shall be modified as per access-point(AP) */
|
||||
/* properties SimpleLink device will connect to following AP when application */
|
||||
/* is executed. */
|
||||
|
||||
/* AP SSID */
|
||||
#define SSID_NAME "SPN24"
|
||||
/* Security type (OPEN or WEP or WPA) */
|
||||
#define SECURITY_TYPE SL_WLAN_SEC_TYPE_OPEN
|
||||
/* Password of the secured AP */
|
||||
#define SECURITY_KEY ""
|
||||
|
||||
#define SSID_AP_MODE "<ap-ssid>"
|
||||
#define SEC_TYPE_AP_MODE SL_WLAN_SEC_TYPE_OPEN
|
||||
#define PASSWORD_AP_MODE ""
|
||||
#include "wifiConf.h"
|
||||
|
||||
/* Loop forever, user can change it as per application's requirement */
|
||||
#define LOOP_FOREVER() \
|
||||
19
src/wifiConf.h.example
Normal file
19
src/wifiConf.h.example
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* wifiConf.h
|
||||
*
|
||||
* Created on: 28 Oct 2020
|
||||
* Author: MReenen
|
||||
*/
|
||||
|
||||
#ifndef SRC_WIFICONF_H_
|
||||
#define SRC_WIFICONF_H_
|
||||
|
||||
/* AP SSID */
|
||||
#define SSID_NAME "<ssid>"
|
||||
/* Security type (OPEN or WEP or WPA) */
|
||||
#define SECURITY_TYPE SL_WLAN_SEC_TYPE_WPA_WPA2
|
||||
//#define SECURITY_TYPE SL_WLAN_SEC_TYPE_OPEN
|
||||
/* Password of the secured AP */
|
||||
#define SECURITY_KEY "<password"
|
||||
|
||||
#endif /* SRC_WIFICONF_H_ */
|
||||
Loading…
x
Reference in New Issue
Block a user