Update revision to use TI's mqtt and Freertos.

Dependencies:   mbed client server

Fork of cc3100_Test_mqtt_CM3 by David Fletcher

Committer:
dflet
Date:
Thu Sep 03 14:02:37 2015 +0000
Revision:
3:a8c249046181
SPI Mode change 1 to 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 3:a8c249046181 1 /******************************************************************************
dflet 3:a8c249046181 2 *
dflet 3:a8c249046181 3 * Copyright (C) 2014 Texas Instruments Incorporated
dflet 3:a8c249046181 4 *
dflet 3:a8c249046181 5 * All rights reserved. Property of Texas Instruments Incorporated.
dflet 3:a8c249046181 6 * Restricted rights to use, duplicate or disclose this code are
dflet 3:a8c249046181 7 * granted through contract.
dflet 3:a8c249046181 8 *
dflet 3:a8c249046181 9 * The program may not be used without the written permission of
dflet 3:a8c249046181 10 * Texas Instruments Incorporated or against the terms and conditions
dflet 3:a8c249046181 11 * stipulated in the agreement under which this program has been supplied,
dflet 3:a8c249046181 12 * and under no circumstances can it be used with non-TI connectivity device.
dflet 3:a8c249046181 13 *
dflet 3:a8c249046181 14 ******************************************************************************/
dflet 3:a8c249046181 15 #include "mbed.h"
dflet 3:a8c249046181 16 #include "mqtt_common.h"
dflet 3:a8c249046181 17 #include "sl_mqtt_server.h"
dflet 3:a8c249046181 18 #include "server_core.h"
dflet 3:a8c249046181 19 #include "client_mgmt.h"
dflet 3:a8c249046181 20 #include "server_util.h"
dflet 3:a8c249046181 21 #include "cc31xx_sl_net.h"
dflet 3:a8c249046181 22 #include "FreeRTOS.h"
dflet 3:a8c249046181 23 #include "osi.h"
dflet 3:a8c249046181 24
dflet 3:a8c249046181 25 namespace mbed_mqtt {
dflet 3:a8c249046181 26
dflet 3:a8c249046181 27 /*-------------------------------------------------------------------------
dflet 3:a8c249046181 28 * MQTT Routines
dflet 3:a8c249046181 29 *-------------------------------------------------------------------------
dflet 3:a8c249046181 30 */
dflet 3:a8c249046181 31 OsiLockObj_t MutexLockObj;
dflet 3:a8c249046181 32
dflet 3:a8c249046181 33 // wrapper function
dflet 3:a8c249046181 34 static void mutex_lock(void* mutex_hndl)
dflet 3:a8c249046181 35 {
dflet 3:a8c249046181 36 osi_LockObjLock((OsiLockObj_t*)mutex_hndl, OSI_WAIT_FOREVER);
dflet 3:a8c249046181 37 }
dflet 3:a8c249046181 38
dflet 3:a8c249046181 39 // wrapper function
dflet 3:a8c249046181 40 static void mutex_unlock(void* mutex_hndl)
dflet 3:a8c249046181 41 {
dflet 3:a8c249046181 42 osi_LockObjUnlock((OsiLockObj_t*)mutex_hndl);
dflet 3:a8c249046181 43 }
dflet 3:a8c249046181 44
dflet 3:a8c249046181 45 struct mqtt_server_lib_cfg server_cfg = {
dflet 3:a8c249046181 46 0,
dflet 3:a8c249046181 47 0,
dflet 3:a8c249046181 48 &MutexLockObj,
dflet 3:a8c249046181 49 mutex_lock,
dflet 3:a8c249046181 50 mutex_unlock,
dflet 3:a8c249046181 51 NULL /*Debug print*/
dflet 3:a8c249046181 52 };
dflet 3:a8c249046181 53
dflet 3:a8c249046181 54 struct mqtt_server_app_cfg app_config = {
dflet 3:a8c249046181 55 NULL
dflet 3:a8c249046181 56 };
dflet 3:a8c249046181 57
dflet 3:a8c249046181 58
dflet 3:a8c249046181 59 /*Task Priority and Response Time*/
dflet 3:a8c249046181 60 uint32_t g_srvr_wait_secs,g_srvr_task_priority;
dflet 3:a8c249046181 61 struct device_net_services net_ops = {comm_open,tcp_send,tcp_recv,
dflet 3:a8c249046181 62 send_dest,recv_from,comm_close,
dflet 3:a8c249046181 63 tcp_listen,tcp_accept,tcp_select,rtc_secs};
dflet 3:a8c249046181 64 void VMqttServerRunTask(const signed char *pvParams);
dflet 3:a8c249046181 65
dflet 3:a8c249046181 66 SlMqttServerCbs_t cbs_obj,*cbs_ptr=NULL;
dflet 3:a8c249046181 67 struct ctrl_struct{
dflet 3:a8c249046181 68 const void* plugin_hndl;
dflet 3:a8c249046181 69 }app_hndl;
dflet 3:a8c249046181 70
dflet 3:a8c249046181 71
dflet 3:a8c249046181 72 uint16_t sl_server_connect_cb (const struct utf8_string *clientId,
dflet 3:a8c249046181 73 const struct utf8_string *username,
dflet 3:a8c249046181 74 const struct utf8_string *password,
dflet 3:a8c249046181 75 void **app_usr)
dflet 3:a8c249046181 76 {
dflet 3:a8c249046181 77 return( cbs_obj.sl_ExtLib_MqttConn((const char*)MQ_CONN_UTF8_BUF(clientId),
dflet 3:a8c249046181 78 MQ_CONN_UTF8_LEN(clientId), (const char*)MQ_CONN_UTF8_BUF(username),
dflet 3:a8c249046181 79 MQ_CONN_UTF8_LEN(username), (const char*)MQ_CONN_UTF8_BUF(password),
dflet 3:a8c249046181 80 MQ_CONN_UTF8_LEN(password), app_usr));
dflet 3:a8c249046181 81 }
dflet 3:a8c249046181 82
dflet 3:a8c249046181 83 void sl_server_publish_cb(const struct utf8_string *topic,
dflet 3:a8c249046181 84 const uint8_t *payload, uint32_t pay_len,
dflet 3:a8c249046181 85 bool dup, uint8_t qos, bool retain)
dflet 3:a8c249046181 86 {
dflet 3:a8c249046181 87 cbs_obj.sl_ExtLib_MqttRecv((const char*)MQ_CONN_UTF8_BUF(topic),
dflet 3:a8c249046181 88 MQ_CONN_UTF8_LEN(topic), payload, pay_len, dup, qos, retain);
dflet 3:a8c249046181 89 }
dflet 3:a8c249046181 90
dflet 3:a8c249046181 91 void sl_server_disconn_cb(const void *app_usr, bool due2err)
dflet 3:a8c249046181 92 {
dflet 3:a8c249046181 93 cbs_obj.sl_ExtLib_MqttDisconn((void*)app_usr, due2err);
dflet 3:a8c249046181 94 }
dflet 3:a8c249046181 95
dflet 3:a8c249046181 96 int32_t sl_ExtLib_MqttServerSend(const char *topic, const void *data, int32_t len,
dflet 3:a8c249046181 97 uint8_t qos, bool retain, uint32_t flags)
dflet 3:a8c249046181 98 {
dflet 3:a8c249046181 99 struct utf8_string topic_utf8={NULL};
dflet 3:a8c249046181 100 topic_utf8.buffer = (char*)topic;
dflet 3:a8c249046181 101 topic_utf8.length = strlen(topic);
dflet 3:a8c249046181 102 return( mqtt_server_app_pub_send(&topic_utf8,(const uint8_t*)data, len,
dflet 3:a8c249046181 103 (enum mqtt_qos)qos, retain));
dflet 3:a8c249046181 104 }
dflet 3:a8c249046181 105
dflet 3:a8c249046181 106 int32_t sl_ExtLib_MqttTopicEnroll(const char *topic)
dflet 3:a8c249046181 107 {
dflet 3:a8c249046181 108 struct utf8_string topic_utf8={NULL};
dflet 3:a8c249046181 109 topic_utf8.buffer = (char*)topic;
dflet 3:a8c249046181 110 topic_utf8.length = strlen(topic);
dflet 3:a8c249046181 111 return( mqtt_server_topic_enroll((void*)(app_hndl.plugin_hndl),
dflet 3:a8c249046181 112 &topic_utf8,MQTT_QOS2));
dflet 3:a8c249046181 113
dflet 3:a8c249046181 114 }
dflet 3:a8c249046181 115
dflet 3:a8c249046181 116 int32_t sl_ExtLib_MqttTopicDisenroll(const char *topic)
dflet 3:a8c249046181 117 {
dflet 3:a8c249046181 118 struct utf8_string topic_utf8={NULL};
dflet 3:a8c249046181 119 topic_utf8.buffer = (char*)topic;
dflet 3:a8c249046181 120 topic_utf8.length = strlen(topic);
dflet 3:a8c249046181 121 return( mqtt_server_topic_disenroll((void*)&(app_hndl.plugin_hndl),
dflet 3:a8c249046181 122 &topic_utf8));
dflet 3:a8c249046181 123 }
dflet 3:a8c249046181 124
dflet 3:a8c249046181 125 struct mqtt_server_app_cbs server_appcallbacks =
dflet 3:a8c249046181 126 {
dflet 3:a8c249046181 127 sl_server_connect_cb,
dflet 3:a8c249046181 128 sl_server_publish_cb,
dflet 3:a8c249046181 129 sl_server_disconn_cb
dflet 3:a8c249046181 130 };
dflet 3:a8c249046181 131
dflet 3:a8c249046181 132 int32_t sl_ExtLib_MqttServerInit(const SlMqttServerCfg_t *cfg,
dflet 3:a8c249046181 133 const SlMqttServerCbs_t *app_cbs)
dflet 3:a8c249046181 134 {
dflet 3:a8c249046181 135
dflet 3:a8c249046181 136 int32_t ret;
dflet 3:a8c249046181 137
dflet 3:a8c249046181 138 //valid loopback port has to be specified for correct operations
dflet 3:a8c249046181 139 if(cfg->loopback_port == 0)
dflet 3:a8c249046181 140 {
dflet 3:a8c249046181 141 return -1;
dflet 3:a8c249046181 142 }
dflet 3:a8c249046181 143 server_cfg.listener_port=cfg->server_info.port_number;
dflet 3:a8c249046181 144 server_cfg.debug_printf = cfg->dbg_print;
dflet 3:a8c249046181 145 cbs_ptr=&cbs_obj;
dflet 3:a8c249046181 146 memcpy(cbs_ptr,app_cbs,sizeof(SlMqttServerCbs_t ));
dflet 3:a8c249046181 147
dflet 3:a8c249046181 148 g_srvr_task_priority=(uint32_t)cfg->rx_tsk_priority;
dflet 3:a8c249046181 149 g_srvr_wait_secs=(uint32_t)cfg->resp_time;
dflet 3:a8c249046181 150 ret = osi_LockObjCreate(&MutexLockObj);
dflet 3:a8c249046181 151 if(!ret)
dflet 3:a8c249046181 152 {
dflet 3:a8c249046181 153 ret=mqtt_server_init(&server_cfg, &app_config);
dflet 3:a8c249046181 154 }
dflet 3:a8c249046181 155 else
dflet 3:a8c249046181 156 {
dflet 3:a8c249046181 157 ret = osi_LockObjDelete(&MutexLockObj);
dflet 3:a8c249046181 158 return(-1);
dflet 3:a8c249046181 159 }
dflet 3:a8c249046181 160 /* registering the device specific implementations for net operations */
dflet 3:a8c249046181 161 mqtt_server_register_net_svc(&net_ops);
dflet 3:a8c249046181 162
dflet 3:a8c249046181 163 /* registering the apps callbacks */
dflet 3:a8c249046181 164 app_hndl.plugin_hndl = mqtt_server_app_register(&server_appcallbacks, "temp");
dflet 3:a8c249046181 165
dflet 3:a8c249046181 166 /* start the Server Run task */
dflet 3:a8c249046181 167
dflet 3:a8c249046181 168 // osi_TaskCreate(VMqttServerRunTask, (const signed char *) "MQTTServerRun", 2048, NULL, g_srvr_task_priority, NULL );
dflet 3:a8c249046181 169
dflet 3:a8c249046181 170 return ret;
dflet 3:a8c249046181 171 }
dflet 3:a8c249046181 172
dflet 3:a8c249046181 173 void VMqttServerRunTask(void *pvParams)
dflet 3:a8c249046181 174 {
dflet 3:a8c249046181 175 mqtt_server_run(g_srvr_wait_secs);
dflet 3:a8c249046181 176 }
dflet 3:a8c249046181 177
dflet 3:a8c249046181 178 }//namespace mbed_mqtt
dflet 3:a8c249046181 179
dflet 3:a8c249046181 180