Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <api_os.h>
- #include <api_fs.h>
- #include <api_event.h>
- #include <api_socket.h>
- #include <api_network.h>
- #include <api_debug.h>
- #include <api_hal_uart.h>
- #include <api_gps.h>
- #include <api_info.h>
- #include <api_hal_gpio.h>
- #include <api_hal_pm.h>
- #include "buffer.h"
- #include "gps_parse.h"
- #include "math.h"
- #include "gps.h"
- #define LOG_FILE_NAME "/t/log.csv"
- #define VIOLATIONS_FILE_NAME "/t/vlts.csv"
- #define TMP_FILE_NAME "/t/tmp.csv"
- #define SERVER_ADDRESS "80.211.88.87"
- #define SERVER_PORT 4000
- #define RECEIVE_BUFFER_MAX_LENGTH (1 * 1024)
- #define FS_BUFFER_MAX_LENGTH 400
- #define MAIN_TASK_STACK_SIZE (1024 * 2)
- #define MAIN_TASK_PRIORITY 0
- #define MAIN_TASK_NAME "Main Task"
- #define UPDATE_TASK_STACK_SIZE (1024 * 2)
- #define UPDATE_TASK_PRIORITY 0
- #define UPDATE_TASK_NAME "Update Task"
- #define GPS_TASK_STACK_SIZE (1024 * 4)
- #define GPS_TASK_PRIORITY 1
- #define GPS_TASK_NAME "Gps Task"
- #define HTTP_TASK_STACK_SIZE (1024 * 8)
- #define HTTP_TASK_PRIORITY 2
- #define HTTP_TASK_NAME "Http Task"
- #define MAX_SPEED 80
- #define NUMBER_PLATE "KBG 223G"
- #define VENDOR "SAFARIWATCH"
- static HANDLE mainTaskHandle = NULL;
- bool networkActiveFlag = false;
- static uint8_t preSmsEventsCount = 0;
- bool httpFailed = false;
- char httpBuffer[RECEIVE_BUFFER_MAX_LENGTH], fsBuffer[FS_BUFFER_MAX_LENGTH], imei[16];
- int serverSocket = -1;
- HANDLE connectionSem = NULL;
- HANDLE transmissionSem = NULL;
- HANDLE gpsSem = NULL;
- int errorCode = 0;
- GPIO_LEVEL contactPinState = GPIO_LEVEL_HIGH;
- double latVal = 0, lngVal = 0;
- int Y, M, D, hr, mn, sc;
- int frequency = 0;
- float speed;
- int printData = 0;
- GPIO_config_t relayPin, buzzerPin;
- const char* FILE_HEADER = "Date,Time,IMEI,Vendor,Number Plate,Speed,Latitude,Longitude,Power Disconnect,Speed Disconnect\r\n";
- const char* REQUEST = "%d/%d/%d,%d:%d:%d,%s,%s,%s,%.2f,%f%c,%f%c,%d,%d";
- const int MAX_LINE_LENGTH = 200;
- void update_time_variables(void) {
- RTC_Time_t time;
- TIME_GetRtcTIme(&time);
- Y = time.year;
- M = time.month;
- D = time.day;
- hr = time.hour >= 24 ? time.hour - 24 : time.hour;
- mn = time.minute;
- sc = time.second;
- if (time.timeZone > 0)
- hr += time.timeZone;
- }
- void update_imei(void) {
- memset(imei, '\0', sizeof(imei));
- INFO_GetIMEI(imei);
- }
- bool init_file(const char* filePath) {
- Trace(1,"init file");
- int32_t logFptr, result;
- logFptr = API_FS_Open(filePath, FS_O_WRONLY|FS_O_CREAT|FS_O_EXCL, 0);
- if (logFptr == -1) {
- Trace(1, "%s file found!", filePath);
- return true;
- } else if (logFptr < 0) {
- Trace(1,"open file %s error:%d", filePath, logFptr);
- return false;
- } else {
- // write header
- result = API_FS_Write(logFptr, (uint8_t *)FILE_HEADER, strlen(FILE_HEADER));
- if (result < 0) {
- Trace(1,"write file %s error:%d", filePath,result);
- }
- API_FS_Close(logFptr);
- return true;
- }
- return true;
- }
- void save_data(const char* filePath) {
- if (init_file(filePath) == true) {
- Trace(1, "Open file: %s", filePath);
- int logFptr = API_FS_Open(filePath, FS_O_WRONLY|FS_O_CREAT|FS_O_APPEND, 0);
- if (logFptr < 0) {
- Trace(1,"open file %s error:%d", filePath, logFptr);
- return;
- }
- Trace(1, "Clear fs buffer");
- memset(fsBuffer, '\0', sizeof(fsBuffer));
- char latHem = latVal < 0 ? 'W' : 'E';
- char lngHem = lngVal < 0 ? 'S' : 'N';
- Trace(1, "sprintf");
- sprintf(
- // "Date,Time,IMEI,Vendor,Number Plate,Speed,Latitude,Longitude,Power Disconnect,Speed Disconnect\r\n";
- fsBuffer, "%d/%d/%d,%d:%d:%d,%s,%s,%s,%.2f,%f%c,%f%c,%d,%d\r\n",
- Y, M, D,
- hr, mn, sc,
- imei, VENDOR, NUMBER_PLATE,
- speed,
- latVal, latHem, lngVal, lngHem,
- 0, 0
- );
- Trace(1, "write");
- int result = API_FS_Write(logFptr, fsBuffer, strlen(fsBuffer));
- if (result < 0) {
- Trace(1,"write file %s error:%d", filePath, result);
- } else {
- Trace(1, "Save %s data: Successful!", filePath);
- }
- Trace(1, "close");
- API_FS_Close(logFptr);
- };
- }
- int32_t time_to_seconds(int h, int m, int s) {
- return (h * 3600) + (m * 60) + s;
- }
- void print_data(void) {
- Trace(1, "Print data.");
- Trace(1, "Open vioations file.");
- int vltsFptr = API_FS_Open(VIOLATIONS_FILE_NAME, FS_O_RDONLY, 0);
- if (vltsFptr < 0) {
- Trace(1, "open file %s error:%d", VIOLATIONS_FILE_NAME, vltsFptr);
- return;
- }
- Trace(1, "Open tmp file.");
- int tmpFptr = API_FS_Open(TMP_FILE_NAME, FS_O_WRONLY|FS_O_CREAT|FS_O_APPEND, 0);
- if (tmpFptr < 0) {
- Trace(1, "open file %s error:%d", TMP_FILE_NAME, tmpFptr);
- return;
- }
- char lineBuffer[MAX_LINE_LENGTH], *sepBuffer = NULL;
- memset(fsBuffer, '\0', sizeof(fsBuffer));
- int result, i = 0, nextLineStart = 0;
- char *token = NULL, *tailPtr = NULL, *newlinePtr = NULL;
- long int dateTime[6];
- long int currentTime = time_to_seconds(hr, mn, sc), logTime;
- Trace(1, "Enter while.");
- while (1) {
- Trace(1, "Read vioations file.");
- result = API_FS_Read(vltsFptr, fsBuffer, FS_BUFFER_MAX_LENGTH);
- Trace(1, "Clear previous memory if any.");
- Trace(1, "Allocate memory for sep buffer");
- sepBuffer = malloc((strlen(fsBuffer) + 1) * sizeof(char));
- Trace(1, "Copy fs buffer to sep buffer");
- strcpy(sepBuffer, fsBuffer);
- if (result < 0) {
- Trace(1, "read file %s error:%d", VIOLATIONS_FILE_NAME, result);
- break;
- } else if (result == 0) {
- Trace(1, "Nothing more to read!");
- break;
- }
- Trace(1, "Search last line.");
- // find the location of the first newline character
- newlinePtr = strrchr(fsBuffer, '\n');
- // if the newline character is at position 1, read from next position
- if (newlinePtr == fsBuffer) {
- newlinePtr += 1;
- break;
- }
- Trace(1, "%d - %d = %d", newlinePtr, fsBuffer, (newlinePtr - fsBuffer));
- Trace(1, "Added: %d", (newlinePtr - fsBuffer));
- // get the start location of the next line
- nextLineStart += (newlinePtr - fsBuffer) / sizeof(char);
- Trace(1, "Next line start: %d", nextLineStart);
- // rewind the file pointer
- Trace(1, "Seeking.");
- result = API_FS_Seek(vltsFptr, nextLineStart, FS_SEEK_SET);
- if (result != 0) {
- Trace(1, "Seek file: %s Result: %d", VIOLATIONS_FILE_NAME, result);
- }
- // seek to file end if seek result is not equal to expected value
- if (result != nextLineStart) {
- result = API_FS_Seek(vltsFptr, 0, FS_SEEK_END);
- Trace(1, "Seek file: %s Result: %d", VIOLATIONS_FILE_NAME, result);
- }
- result = 0;
- Trace(1, "Begin parse lines");
- // parse the read lines
- do {
- // obtain the first 6 tokens which represent the log's date and time
- for (i = 0; i < 6; i++) {
- // separate using time and date and csv delimiters
- token = strsep(&sepBuffer, "/:,");
- // check for separation failure
- if (token == NULL) {
- Trace(1, "parsing failed! null token.");
- result = -1;
- break;
- }
- // parse tokens to integers
- dateTime[i] = strtol(token, &tailPtr, 10);
- // check for parsing failures
- if (*tailPtr != '\0') {
- Trace(1, "parsing failed! expected null byte but got %c", *tailPtr);
- result = -1;
- break;
- }
- }
- // break if a parsing error occured
- if (result == -1) break;
- Trace(1, "%ld/%ld/%ld,%ld:%ld:%ld", dateTime[0], dateTime[1], dateTime[2], dateTime[3], dateTime[4], dateTime[5]);
- // obtain the rest of the line
- token = strsep(&sepBuffer, "\n");
- // break if '\n' character is not found
- if (token == NULL) {
- Trace(1, "eol not found! continuing.");
- break;
- }
- // get the log time in seconds
- logTime = time_to_seconds(dateTime[3], dateTime[4], dateTime[5]);
- // if log time is within the past hour or between 00:00 and 01:00
- if (((currentTime - logTime) < 3600) || (logTime < 3600)) {
- // discard entries of a different day
- if (dateTime[2] != D) continue;
- // construct the new line
- sprintf(
- lineBuffer, "ENTRY: %d/%d/%d,%d:%d:%d,%s\n",
- dateTime[0], dateTime[1], dateTime[2], dateTime[3], dateTime[4], dateTime[5],
- token
- );
- // print the line
- UART_Write(UART1, lineBuffer, strlen(lineBuffer));
- // write the file to the file tmp.csv
- result = API_FS_Write(tmpFptr, lineBuffer, strlen(lineBuffer));
- if (result < 0) {
- Trace(1, "write file %s error:%d", TMP_FILE_NAME, result);
- } else {
- Trace(1, "Save %s data: Successful!", TMP_FILE_NAME);
- }
- }
- } while (token != NULL);
- OS_Sleep(10);
- }
- // close files
- API_FS_Close(tmpFptr);
- API_FS_Close(vltsFptr);
- // remove file: vlts.csv
- result = API_FS_Delete(VIOLATIONS_FILE_NAME);
- if (result != 0) {
- Trace(1, "delete file %s error:%d", VIOLATIONS_FILE_NAME, result);
- }
- // rename file: tmp.csv to: vlts.csv
- result = API_FS_Rename(TMP_FILE_NAME, VIOLATIONS_FILE_NAME);
- if (result != 0) {
- Trace(1, "rename file %s error:%d", TMP_FILE_NAME, result);
- }
- }
- void EventDispatch(API_Event_t* pEvent) {
- switch(pEvent->id) {
- case API_EVENT_ID_NO_SIMCARD:
- Trace(10,"!!NO SIM CARD%d!!!!",pEvent->param1);
- break;
- case API_EVENT_ID_SYSTEM_READY:
- Trace(1,"system initialize complete");
- preSmsEventsCount |= 1;
- break;
- case API_EVENT_ID_NETWORK_REGISTERED_HOME:
- case API_EVENT_ID_NETWORK_REGISTERED_ROAMING:
- Trace(2,"network register success");
- preSmsEventsCount |= 2;
- Network_StartAttach();
- break;
- case API_EVENT_ID_NETWORK_ATTACHED:
- Trace(2,"network attach success");
- Network_PDP_Context_t context = {
- .apn ="internet",
- .userName = "",
- .userPasswd = ""
- };
- Network_StartActive(context);
- break;
- case API_EVENT_ID_GPS_UART_RECEIVED:
- GPS_Update(pEvent->pParam1,pEvent->param1);
- break;
- case API_EVENT_ID_NETWORK_ACTIVATED:
- Trace(2,"network activate success");
- connectionSem = 1;
- networkActiveFlag = true;
- break;
- case API_EVENT_ID_SOCKET_CONNECTED:
- Trace(2,"event connect");
- connectionSem = 1;
- break;
- case API_EVENT_ID_SOCKET_SENT:
- connectionSem = 1;
- break;
- case API_EVENT_ID_SOCKET_RECEIVED:
- {
- Trace(1, "Socket received!");
- // int fd = pEvent->param1;
- // int length = pEvent->param2 > RECEIVE_BUFFER_MAX_LENGTH
- // ? RECEIVE_BUFFER_MAX_LENGTH
- // : pEvent->param2;
- // memset(httpBuffer, '\0', sizeof(httpBuffer));
- // length = Socket_TcpipRead(fd, httpBuffer, length);
- // Trace(2,"socket %d received %d bytes", fd, length);
- // UART_Write(UART1, "\r\n\r\n", strlen("\r\n\r\n"));
- // UART_Write(UART1, httpBuffer, strlen(httpBuffer));
- // transmissionSem = 0;
- break;
- }
- case API_EVENT_ID_SOCKET_CLOSED:
- {
- int fd = pEvent->param1;
- Trace(2,"socket %d closed", fd);
- // serverSocket = -1;
- connectionSem = 1;
- break;
- }
- case API_EVENT_ID_SOCKET_ERROR:
- {
- int fd = pEvent->param1;
- Trace(2,"socket %d error occurred,cause:%d",fd,pEvent->param2);
- errorCode = pEvent->param2;
- connectionSem = 1;
- break;
- }
- default:
- break;
- }
- }
- void CreateSem(HANDLE* sem_) {
- *sem_ = 0;
- }
- void WaitSem(HANDLE* sem_) {
- while(*sem_ == 0)
- OS_Sleep(1);
- *sem_ = 0;
- }
- bool Connect() {
- // reset httpBuffer
- memset(httpBuffer, 0, sizeof(httpBuffer));
- // resolve server IP address
- if(DNS_GetHostByName2(SERVER_ADDRESS, (char*)httpBuffer) != 0)
- return false;
- // print server domain name and ip address
- Trace(2, "DNS,domain: %s, ip: %s, strlen(ip): %d", SERVER_ADDRESS, httpBuffer,
- strlen(httpBuffer));
- // create a blocked semaphore
- CreateSem(&connectionSem);
- // connect to the server
- serverSocket = Socket_TcpipConnect(UDP, httpBuffer, SERVER_PORT);
- Trace(2, "connect tcp server, socketFd: %d", serverSocket);
- // wait for the connection to complete or fail
- // WaitSem(&connectionSem);
- // print the end of the connection
- Trace(2, "connect end");
- // handle connection fail
- if(errorCode != 0) {
- // reset error code
- errorCode = 0;
- // print error message
- Trace(2, "error ocurred");
- // terminate function
- return false;
- }
- // terminate function
- return true;
- }
- bool Write(uint8_t* data, uint16_t len) {
- // print to signal function start
- Trace(2, "Write");
- // create a blocked semaphore
- CreateSem(&connectionSem);
- // write data to the server socket
- int ret = Socket_TcpipWrite(serverSocket, data, len);
- // handle write fail
- if(ret <= 0) {
- // print to signal write fail
- Trace(2, "socket write fail: %d", ret);
- // terminate function
- return false;
- }
- // print to signal write success
- Trace(2,"### socket %d send %d bytes data to server: %s, ret: %d",
- serverSocket, len, data, ret);
- // wait for response data from the server
- // WaitSem(&connectionSem);
- // print to signal write end
- Trace(2,"### write end");
- // handle write fail
- if(errorCode != 0) {
- // reset error code
- errorCode = 0;
- // print to signal write fail
- Trace(2,"error ocurred");
- // terminate function
- return false;
- }
- // terminate function
- return true;
- }
- bool Close() {
- // create blocked semaphore
- CreateSem(&connectionSem);
- // close the socket
- Socket_TcpipClose(serverSocket);
- // wait for the socket to be closed
- WaitSem(&connectionSem);
- // reset the value of serverSocket to -1
- serverSocket = -1;
- // terminate function
- return true;
- }
- void httpTask(void* param) {
- // -------------- LED & BTN CONFIG -----------------
- GPIO_config_t gpioLedBlue = {
- .mode = GPIO_MODE_OUTPUT,
- .pin = GPIO_PIN28,
- .defaultLevel = GPIO_LEVEL_LOW
- };
- PM_PowerEnable(POWER_TYPE_VPAD ,true);
- GPIO_Init(gpioLedBlue);
- // -------------- END CONFIG -----------------
- bool writeSuccess = false;
- // wait for network to be active
- WaitSem(&connectionSem);
- // print semaphore status
- Trace(2, "sem: %d, %p", (int)connectionSem, (void*)connectionSem);
- // infinite loop
- while(1) {
- Trace(1, "Waiting for transmission sem.");
- // wait for previous transmission to complete
- WaitSem(&transmissionSem);
- // turn on the LED
- GPIO_SetLevel(gpioLedBlue, GPIO_LEVEL_HIGH);
- // signal http task running
- Trace(1, "Http task running ...");
- // print to signal connect start
- Trace(1, "start connect now");
- // connect to server if a connection does not already exist
- if (serverSocket == -1) {
- // send coordinates via SMS if connection fails
- if (!Connect()) {
- // signal transmission over http fail
- httpFailed = true;
- continue;
- }
- }
- // create the request string
- char latHem = latVal < 0 ? 'W' : 'E';
- char lngHem = lngVal < 0 ? 'S' : 'N';
- memset(httpBuffer, '\0', sizeof(httpBuffer));
- sprintf(
- httpBuffer, REQUEST,
- Y, M, D,
- hr, mn, sc,
- imei, VENDOR, NUMBER_PLATE,
- speed,
- latVal, latHem, lngVal, lngHem,
- 0, 0
- );
- // write data to the server
- writeSuccess = Write(httpBuffer, strlen(httpBuffer));
- // if write attempt fails
- if(!writeSuccess) {
- // signal transmission over http fail
- httpFailed = true;
- // close server socket
- Close();
- }
- // turn off the LED
- GPIO_SetLevel(gpioLedBlue, GPIO_LEVEL_LOW);
- }
- }
- void gpsTask(void *pData) {
- GPS_Info_t* gpsInfo = Gps_GetInfo();
- // Wait for SMS and gprs connections to be active
- while(!networkActiveFlag) {
- Trace(1,"Waiting for GPRS connection to be active");
- OS_Sleep(2000);
- }
- //open GPS hardware(UART2 open either)
- GPS_Init();
- GPS_Open(NULL);
- //wait for gps start up, or gps will not response command
- while(gpsInfo->rmc.latitude.value == 0)
- OS_Sleep(1000);
- // signal GPS initialization okay
- Trace(1," GPS init ok");
- // infinite loop
- while(1) {
- //show fix info
- uint8_t isFixed = gpsInfo->gsa[0].fix_type > gpsInfo->gsa[1].fix_type
- ? gpsInfo->gsa[0].fix_type
- : gpsInfo->gsa[1].fix_type;
- char* isFixedStr;
- if(isFixed == 2)
- isFixedStr = "2D fix";
- else if(isFixed == 3)
- {
- if(gpsInfo->gga.fix_quality == 1)
- isFixedStr = "3D fix";
- else if(gpsInfo->gga.fix_quality == 2)
- isFixedStr = "3D/DGPS fix";
- }
- else
- isFixedStr = "no fix";
- //convert unit ddmm.mmmm to degree(°)
- // obtain the current latitude and update the global latitude variable
- int temp = (int)(gpsInfo->rmc.latitude.value/gpsInfo->rmc.latitude.scale/100);
- latVal = temp+(double)(gpsInfo->rmc.latitude.value - temp*gpsInfo->rmc.latitude.scale*100)/gpsInfo->rmc.latitude.scale/60.0;
- // obtain the current longitude and update the global longitude variable
- temp = (int)(gpsInfo->rmc.longitude.value/gpsInfo->rmc.longitude.scale/100);
- lngVal = temp+(double)(gpsInfo->rmc.longitude.value - temp*gpsInfo->rmc.longitude.scale*100)/gpsInfo->rmc.longitude.scale/60.0;
- gpsSem = 1;
- OS_Sleep(1000);
- }
- }
- void updateTask(void* param) {
- while(1) {
- // wait for the gps
- WaitSem(&gpsSem);
- // print data if the print button has been pressed
- if (printData == 1) {
- print_data();
- printData = 0;
- }
- // update the time variables
- update_time_variables();
- // update imei
- update_imei();
- // save the data
- save_data(LOG_FILE_NAME);
- // log entry if it is a vioation
- if (speed > MAX_SPEED) {
- save_data(VIOLATIONS_FILE_NAME);
- }
- // signal transmit data to server
- transmissionSem = 1;
- OS_Sleep(5000);
- }
- }
- void OnPinFalling(GPIO_INT_callback_param_t* param) {
- // Trace(1,"OnPinFalling");
- switch(param->pin)
- {
- case GPIO_PIN3:
- frequency++;
- break;
- case GPIO_PIN2:
- printData = 1;
- break;
- default:
- break;
- }
- }
- void sampleSpeed(void* param) {
- speed = frequency * 1.365;
- Trace(1, "Speed: %f", speed);
- // alert if max speed has been exceeded
- if (speed > MAX_SPEED) {
- GPIO_SetLevel(buzzerPin, GPIO_LEVEL_HIGH);
- GPIO_SetLevel(relayPin, GPIO_LEVEL_HIGH);
- } else {
- GPIO_SetLevel(buzzerPin, GPIO_LEVEL_LOW);
- GPIO_SetLevel(relayPin, GPIO_LEVEL_LOW);
- }
- // reset frequency
- frequency = 0;
- // reschedule the speed sampler for per second
- OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
- }
- void mainTask(void *pData) {
- // set up network time synchronization
- TIME_SetIsAutoUpdateRtcTime(true);
- // create event object
- API_Event_t* event=NULL;
- // configure UART
- UART_Config_t config = {
- .baudRate = UART_BAUD_RATE_9600,
- .dataBits = UART_DATA_BITS_8,
- .stopBits = UART_STOP_BITS_1,
- .parity = UART_PARITY_NONE,
- .rxCallback = NULL,
- .useEvent = false
- };
- // initialize UART
- UART_Init(UART1, config);
- // test uart
- UART_Write(UART1, "Hello, world!\r\n", strlen("Hello, world!\r\n"));
- // enable power for used pins
- PM_PowerEnable(POWER_TYPE_VPAD, true);
- PM_PowerEnable(POWER_TYPE_LCD, true);
- // configure speed pin
- GPIO_config_t intPin = {
- .mode = GPIO_MODE_INPUT_INT,
- .pin = GPIO_PIN3,
- .defaultLevel = GPIO_LEVEL_LOW,
- .intConfig.debounce = 0,
- .intConfig.type = GPIO_INT_TYPE_FALLING_EDGE,
- .intConfig.callback = OnPinFalling
- };
- GPIO_Init(intPin);
- // configure print pin
- intPin.intConfig.debounce = 200;
- intPin.pin = GPIO_PIN2;
- GPIO_Init(intPin);
- // configure buzzer pin
- buzzerPin.mode = GPIO_MODE_OUTPUT;
- buzzerPin.pin = GPIO_PIN26;
- buzzerPin.defaultLevel = GPIO_LEVEL_LOW;
- GPIO_Init(buzzerPin);
- // configure relay pin
- relayPin.mode = GPIO_MODE_OUTPUT;
- relayPin.pin = GPIO_PIN29;
- relayPin.defaultLevel = GPIO_LEVEL_LOW;
- GPIO_Init(relayPin);
- // configure engine pin
- GPIO_config_t enginePin = {
- .mode = GPIO_MODE_INPUT,
- .pin = GPIO_PIN30,
- .defaultLevel = GPIO_LEVEL_LOW
- };
- GPIO_Init(enginePin);
- // schedule the speed sampler for per second
- OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
- // create blocked semaphores
- CreateSem(&connectionSem);
- CreateSem(&transmissionSem);
- CreateSem(&gpsSem);
- // create the gpsTask
- OS_CreateTask(gpsTask,
- NULL, NULL, GPS_TASK_STACK_SIZE, GPS_TASK_PRIORITY, 0, 0, GPS_TASK_NAME);
- // wait for 5 seconds
- OS_Sleep(5000);
- // create the httpTask
- OS_CreateTask(httpTask,
- NULL, NULL, HTTP_TASK_STACK_SIZE, HTTP_TASK_PRIORITY, 0, 0, HTTP_TASK_NAME);
- // create the update task
- OS_CreateTask(updateTask,
- NULL, NULL, UPDATE_TASK_STACK_SIZE, UPDATE_TASK_PRIORITY, 0, 0, UPDATE_TASK_NAME);
- // infinite loop
- while(1) {
- // if an event is received
- if(OS_WaitEvent(mainTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER)) {
- // signal the event
- EventDispatch(event);
- // clean up
- OS_Free(event->pParam1);
- OS_Free(event->pParam2);
- OS_Free(event);
- }
- }
- }
- void speed_governor_5_Main(void) {
- // create the main task
- mainTaskHandle = OS_CreateTask(mainTask,
- NULL, NULL, MAIN_TASK_STACK_SIZE, MAIN_TASK_PRIORITY, 0, 0, MAIN_TASK_NAME);
- // set up the main task to receive messages
- OS_SetUserMainHandle(&mainTaskHandle);
- }
Advertisement
Add Comment
Please, Sign In to add comment