Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTFreeRTOS.c
00001 /******************************************************************************* 00002 * Copyright (c) 2014, 2015 IBM Corp. 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Allan Stockdill-Mander - initial API and implementation and/or initial documentation 00015 * Ian Craggs - convert to FreeRTOS 00016 *******************************************************************************/ 00017 00018 #include "MQTTFreeRTOS.h" 00019 00020 00021 int ThreadStart(Thread* thread, void (*fn)(void*), void* arg) 00022 { 00023 int rc = 0; 00024 uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5); 00025 UBaseType_t uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/ 00026 00027 rc = xTaskCreate(fn, /* The function that implements the task. */ 00028 "MQTTTask", /* Just a text name for the task to aid debugging. */ 00029 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */ 00030 arg, /* The task parameter, not used in this case. */ 00031 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */ 00032 &thread->task); /* The task handle is not used. */ 00033 00034 return rc; 00035 } 00036 00037 00038 void MutexInit(Mutex* mutex) 00039 { 00040 mutex->sem = xSemaphoreCreateMutex(); 00041 } 00042 00043 int MutexLock(Mutex* mutex) 00044 { 00045 return xSemaphoreTake(mutex->sem, portMAX_DELAY); 00046 } 00047 00048 int MutexUnlock(Mutex* mutex) 00049 { 00050 return xSemaphoreGive(mutex->sem); 00051 } 00052 00053 00054 void TimerCountdownMS(Timer* timer, unsigned int timeout_ms) 00055 { 00056 timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */ 00057 vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */ 00058 } 00059 00060 00061 void TimerCountdown(Timer* timer, unsigned int timeout) 00062 { 00063 TimerCountdownMS(timer, timeout * 1000); 00064 } 00065 00066 00067 int TimerLeftMS(Timer* timer) 00068 { 00069 xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */ 00070 return (timer->xTicksToWait < 0) ? 0 : (timer->xTicksToWait * portTICK_PERIOD_MS); 00071 } 00072 00073 00074 char TimerIsExpired(Timer* timer) 00075 { 00076 return xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait) == pdTRUE; 00077 } 00078 00079 00080 void TimerInit(Timer* timer) 00081 { 00082 timer->xTicksToWait = 0; 00083 memset(&timer->xTimeOut, '\0', sizeof(timer->xTimeOut)); 00084 } 00085 00086 00087 int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms) 00088 { 00089 TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */ 00090 TimeOut_t xTimeOut; 00091 int recvLen = 0; 00092 00093 vTaskSetTimeOutState(&xTimeOut); /* Record the time at which this function was entered. */ 00094 do 00095 { 00096 int rc = 0; 00097 00098 FreeRTOS_setsockopt(n->my_socket, 0, FREERTOS_SO_RCVTIMEO, &xTicksToWait, sizeof(xTicksToWait)); 00099 rc = FreeRTOS_recv(n->my_socket, buffer + recvLen, len - recvLen, 0); 00100 if (rc > 0) 00101 recvLen += rc; 00102 else if (rc < 0) 00103 { 00104 recvLen = rc; 00105 break; 00106 } 00107 } while (recvLen < len && xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE); 00108 00109 return recvLen; 00110 } 00111 00112 00113 int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms) 00114 { 00115 TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */ 00116 TimeOut_t xTimeOut; 00117 int sentLen = 0; 00118 00119 vTaskSetTimeOutState(&xTimeOut); /* Record the time at which this function was entered. */ 00120 do 00121 { 00122 int rc = 0; 00123 00124 FreeRTOS_setsockopt(n->my_socket, 0, FREERTOS_SO_RCVTIMEO, &xTicksToWait, sizeof(xTicksToWait)); 00125 rc = FreeRTOS_send(n->my_socket, buffer + sentLen, len - sentLen, 0); 00126 if (rc > 0) 00127 sentLen += rc; 00128 else if (rc < 0) 00129 { 00130 sentLen = rc; 00131 break; 00132 } 00133 } while (sentLen < len && xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE); 00134 00135 return sentLen; 00136 } 00137 00138 00139 void FreeRTOS_disconnect(Network* n) 00140 { 00141 FreeRTOS_closesocket(n->my_socket); 00142 } 00143 00144 00145 void NetworkInit(Network* n) 00146 { 00147 n->my_socket = 0; 00148 n->mqttread = FreeRTOS_read; 00149 n->mqttwrite = FreeRTOS_write; 00150 n->disconnect = FreeRTOS_disconnect; 00151 } 00152 00153 00154 int NetworkConnect(Network* n, char* addr, int port) 00155 { 00156 struct freertos_sockaddr sAddr; 00157 int retVal = -1; 00158 uint32_t ipAddress; 00159 00160 if ((ipAddress = FreeRTOS_gethostbyname(addr)) == 0) 00161 goto exit; 00162 00163 sAddr.sin_port = FreeRTOS_htons(port); 00164 sAddr.sin_addr = ipAddress; 00165 00166 if ((n->my_socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP)) < 0) 00167 goto exit; 00168 00169 if ((retVal = FreeRTOS_connect(n->my_socket, &sAddr, sizeof(sAddr))) < 0) 00170 { 00171 FreeRTOS_closesocket(n->my_socket); 00172 goto exit; 00173 } 00174 00175 exit: 00176 return retVal; 00177 } 00178 00179 00180 #if 0 00181 int NetworkConnectTLS(Network *n, char* addr, int port, SlSockSecureFiles_t* certificates, unsigned char sec_method, unsigned int cipher, char server_verify) 00182 { 00183 SlSockAddrIn_t sAddr; 00184 int addrSize; 00185 int retVal; 00186 unsigned long ipAddress; 00187 00188 retVal = sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET); 00189 if (retVal < 0) { 00190 return -1; 00191 } 00192 00193 sAddr.sin_family = AF_INET; 00194 sAddr.sin_port = sl_Htons((unsigned short)port); 00195 sAddr.sin_addr.s_addr = sl_Htonl(ipAddress); 00196 00197 addrSize = sizeof(SlSockAddrIn_t); 00198 00199 n->my_socket = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_SEC_SOCKET); 00200 if (n->my_socket < 0) { 00201 return -1; 00202 } 00203 00204 SlSockSecureMethod method; 00205 method.secureMethod = sec_method; 00206 retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method)); 00207 if (retVal < 0) { 00208 return retVal; 00209 } 00210 00211 SlSockSecureMask mask; 00212 mask.secureMask = cipher; 00213 retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &mask, sizeof(mask)); 00214 if (retVal < 0) { 00215 return retVal; 00216 } 00217 00218 if (certificates != NULL) { 00219 retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_FILES, certificates->secureFiles, sizeof(SlSockSecureFiles_t)); 00220 if (retVal < 0) 00221 { 00222 return retVal; 00223 } 00224 } 00225 00226 retVal = sl_Connect(n->my_socket, (SlSockAddr_t *)&sAddr, addrSize); 00227 if (retVal < 0) { 00228 if (server_verify || retVal != -453) { 00229 sl_Close(n->my_socket); 00230 return retVal; 00231 } 00232 } 00233 00234 SysTickIntRegister(SysTickIntHandler); 00235 SysTickPeriodSet(80000); 00236 SysTickEnable(); 00237 00238 return retVal; 00239 } 00240 #endif
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2