Mark Radbourne / Mbed 2 deprecated FXOS8700CQ_To_Azure_IoT

Dependencies:   azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers threadapi_rtx_mbed.cpp Source File

threadapi_rtx_mbed.cpp

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include <stdlib.h>
00005 #ifdef _CRTDBG_MAP_ALLOC
00006 #include <crtdbg.h>
00007 #endif
00008 
00009 #include <mbed.h>
00010 #include "azure_c_shared_utility/threadapi.h"
00011 #include "azure_c_shared_utility/xlogging.h"
00012 #include "rtos.h"
00013 
00014 DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
00015 
00016 #define MAX_THREADS 4
00017 #define STACK_SIZE  0x4000
00018 
00019 typedef struct _thread
00020 {
00021     Thread*       thrd;
00022     osThreadId    id;
00023     Queue<int, 1> result;
00024 } mbedThread;
00025 static mbedThread threads[MAX_THREADS] = { 0 };
00026 
00027 typedef struct _create_param
00028 {
00029     THREAD_START_FUNC func;
00030     const void* arg;
00031     mbedThread *p_thread;
00032 } create_param;
00033 
00034 static void thread_wrapper(const void* createParamArg)
00035 {
00036     const create_param* p = (const create_param*)createParamArg;
00037     p->p_thread->id = Thread::gettid();
00038     (*(p->func))((void*)p->arg);
00039     free((void*)p);
00040 }
00041 
00042 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
00043 {
00044     THREADAPI_RESULT result;
00045     if ((threadHandle == NULL) ||
00046         (func == NULL))
00047     {
00048         result = THREADAPI_INVALID_ARG;
00049         LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
00050     }
00051     else
00052     {
00053         size_t slot;
00054         for (slot = 0; slot < MAX_THREADS; slot++)
00055         {
00056             if (threads[slot].id == NULL)
00057                 break;
00058         }
00059 
00060         if (slot < MAX_THREADS)
00061         {
00062             create_param* param = (create_param*)malloc(sizeof(create_param));
00063             if (param != NULL)
00064             {
00065                 param->func = func;
00066                 param->arg = arg;
00067                 param->p_thread = threads + slot;
00068                 threads[slot].thrd = new Thread(thread_wrapper, param, osPriorityNormal, STACK_SIZE);
00069                 *threadHandle = (THREAD_HANDLE)(threads + slot);
00070                 result = THREADAPI_OK;
00071             }
00072             else
00073             {
00074                 result = THREADAPI_NO_MEMORY;
00075                 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
00076             }
00077         }
00078         else
00079         {
00080             result = THREADAPI_NO_MEMORY;
00081             LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
00082         }
00083     }
00084 
00085     return result;
00086 }
00087 
00088 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE thr, int *res)
00089 {
00090     THREADAPI_RESULT result = THREADAPI_OK;
00091     mbedThread* p = (mbedThread*)thr;
00092     printf("ThreadAPI_Join - got mbedThread %x\r\n", p);
00093     if (p)
00094     {
00095         osEvent evt = p->result.get();
00096         printf("ThreadAPI_Join - got osEvent\r\n");
00097         if (evt.status == osEventMessage) {
00098             Thread* t = p->thrd;
00099             if (res)
00100             {
00101                 *res = (int)evt.value.p;
00102             }
00103             printf("ThreadAPI_Join - call terminate\r\n");
00104             
00105             (void)t->terminate();
00106             printf("ThreadAPI_Join - terminate done\r\n");
00107         }
00108         else
00109         {
00110             result = THREADAPI_ERROR;
00111             LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
00112         }
00113     }
00114     else
00115     {
00116         result = THREADAPI_INVALID_ARG;
00117         LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
00118     }
00119     return result;
00120 }
00121 
00122 void ThreadAPI_Exit(int res)
00123 {
00124     mbedThread* p;
00125     for (p = threads; p < &threads[MAX_THREADS]; p++)
00126     {
00127         if (p->id == Thread::gettid())
00128         {
00129             p->result.put((int*)res);
00130             break;
00131         }
00132     }
00133 }
00134 
00135 void ThreadAPI_Sleep(unsigned int millisec)
00136 {
00137     //
00138     // The timer on mbed seems to wrap around 65 seconds. Hmmm.
00139     // So we will do our waits in increments of 30 seconds.
00140     //
00141     const int thirtySeconds = 30000;
00142     int numberOfThirtySecondWaits = millisec / thirtySeconds;
00143     int remainderOfThirtySeconds = millisec % thirtySeconds;
00144     int i;
00145     for (i = 1; i <= numberOfThirtySecondWaits; i++)
00146     {
00147         Thread::wait(thirtySeconds);
00148     }
00149     Thread::wait(remainderOfThirtySeconds);
00150 }