Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 #include "axTransport.h"
AxedaCorp 0:65004368569c 2 #include <stdio.h>
AxedaCorp 0:65004368569c 3 #include <stdlib.h>
AxedaCorp 0:65004368569c 4 #include <string.h>
AxedaCorp 0:65004368569c 5
AxedaCorp 0:65004368569c 6 #include "axStatusCodes.h"
AxedaCorp 0:65004368569c 7 #include "axConstants.h"
AxedaCorp 0:65004368569c 8 #include <math.h>
AxedaCorp 0:65004368569c 9 #include "include_me.h"
AxedaCorp 0:65004368569c 10 #include "mbed.h"
AxedaCorp 0:65004368569c 11
AxedaCorp 0:65004368569c 12 using namespace mts;
AxedaCorp 0:65004368569c 13 char *temp_buff;
AxedaCorp 0:65004368569c 14 int buff_size;
AxedaCorp 0:65004368569c 15
AxedaCorp 0:65004368569c 16 /************************************************************************************************/
AxedaCorp 0:65004368569c 17 /*getEpoch() */
AxedaCorp 0:65004368569c 18 /* */
AxedaCorp 0:65004368569c 19 /*This method should return the number of seconds since January 1st 1970. */
AxedaCorp 0:65004368569c 20 /* */
AxedaCorp 0:65004368569c 21 /*returns long value in seconds */
AxedaCorp 0:65004368569c 22 /************************************************************************************************/
AxedaCorp 0:65004368569c 23 long getEpoch(){
AxedaCorp 0:65004368569c 24 return (long)0;
AxedaCorp 0:65004368569c 25 }
AxedaCorp 0:65004368569c 26
AxedaCorp 0:65004368569c 27
AxedaCorp 0:65004368569c 28 /**************************************************************************************************/
AxedaCorp 0:65004368569c 29 /*ax_print() */
AxedaCorp 0:65004368569c 30 /* */
AxedaCorp 0:65004368569c 31 /*A wrapper method that will allow you to direct the output to a particular stream. Most of the */
AxedaCorp 0:65004368569c 32 /*time the output will just go to printf/std. out. This is called by the axAgent library to print*/
AxedaCorp 0:65004368569c 33 /*error and debug messages. It is reccomended to include processing for the integers AX_DEBUG_MSG*/
AxedaCorp 0:65004368569c 34 /*and AX_ERROR_MSG as the library will frequently use these options if configured. */
AxedaCorp 0:65004368569c 35 /* */
AxedaCorp 0:65004368569c 36 /* msgType: allows for an integer value to control how the data is displayed or written */
AxedaCorp 0:65004368569c 37 /* msg: a pointer to a string containing the data to be printed */
AxedaCorp 0:65004368569c 38 /************************************************************************************************/
AxedaCorp 0:65004368569c 39 void ax_print(int msgType, char *msg){
AxedaCorp 0:65004368569c 40 switch(msgType) {
AxedaCorp 0:65004368569c 41 case AX_DEBUG_MSG:
AxedaCorp 0:65004368569c 42 printf("DEBUG: %s\n", msg);
AxedaCorp 0:65004368569c 43 break;
AxedaCorp 0:65004368569c 44 case AX_ERROR_MSG:
AxedaCorp 0:65004368569c 45 printf("ERR: %s\n", msg);
AxedaCorp 0:65004368569c 46 break;
AxedaCorp 0:65004368569c 47 }
AxedaCorp 0:65004368569c 48
AxedaCorp 0:65004368569c 49 }
AxedaCorp 0:65004368569c 50 /************************************************************************************************/
AxedaCorp 0:65004368569c 51 /*sys_delay() */
AxedaCorp 0:65004368569c 52 /* */
AxedaCorp 0:65004368569c 53 /*A wrapper method that is meant to induce a delay on the system that it running it. Used mostly*/
AxedaCorp 0:65004368569c 54 /*to implement timeouts. */
AxedaCorp 0:65004368569c 55 /* */
AxedaCorp 0:65004368569c 56 /* */
AxedaCorp 0:65004368569c 57 /* */
AxedaCorp 0:65004368569c 58 /************************************************************************************************/
AxedaCorp 0:65004368569c 59 void sys_delay(int seconds){
AxedaCorp 0:65004368569c 60
AxedaCorp 0:65004368569c 61 }
AxedaCorp 0:65004368569c 62
AxedaCorp 0:65004368569c 63 /************************************************************************************************/
AxedaCorp 0:65004368569c 64 /*randInt() */
AxedaCorp 0:65004368569c 65 /* */
AxedaCorp 0:65004368569c 66 /*This function is an abstraction function and should be populated with a call to the host's */
AxedaCorp 0:65004368569c 67 /*equivalent of the random function. */
AxedaCorp 0:65004368569c 68 /************************************************************************************************/
AxedaCorp 0:65004368569c 69 int randInt() {
AxedaCorp 0:65004368569c 70 //call to the local create random method.
AxedaCorp 0:65004368569c 71 srand(1235);
AxedaCorp 0:65004368569c 72 return rand();
AxedaCorp 0:65004368569c 73 }
AxedaCorp 0:65004368569c 74
AxedaCorp 0:65004368569c 75 /************************************************************************************************/
AxedaCorp 0:65004368569c 76 /*seedRand() */
AxedaCorp 0:65004368569c 77 /* */
AxedaCorp 0:65004368569c 78 /*This function is an abstraction function and should be populated with a call to the host's */
AxedaCorp 0:65004368569c 79 /*equivalent of the function that seeds the random number generator */
AxedaCorp 0:65004368569c 80 /************************************************************************************************/
AxedaCorp 0:65004368569c 81 int seedRand(){
AxedaCorp 0:65004368569c 82
AxedaCorp 0:65004368569c 83
AxedaCorp 0:65004368569c 84 }
AxedaCorp 0:65004368569c 85
AxedaCorp 0:65004368569c 86 /*************************************************************************************************/
AxedaCorp 0:65004368569c 87 /*net_socketInit() */
AxedaCorp 0:65004368569c 88 /* */
AxedaCorp 0:65004368569c 89 /*This method provides a convenient way to call code that initializes the socket before it is */
AxedaCorp 0:65004368569c 90 /*actually called to open. For example, it could initialize a holder object that you code or set */
AxedaCorp 0:65004368569c 91 /*options on the socket itself. The return will be a code that could be passed up through the */
AxedaCorp 0:65004368569c 92 /*HTTP library and to the error stream. */
AxedaCorp 0:65004368569c 93 /*************************************************************************************************/
AxedaCorp 0:65004368569c 94 int net_socketInit(ax_socket *sock) {
AxedaCorp 0:65004368569c 95 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 96
AxedaCorp 0:65004368569c 97 buff_size=0;
AxedaCorp 0:65004368569c 98 temp_buff=NULL;
AxedaCorp 0:65004368569c 99
AxedaCorp 0:65004368569c 100 return retVal;
AxedaCorp 0:65004368569c 101 }
AxedaCorp 0:65004368569c 102
AxedaCorp 0:65004368569c 103 /*************************************************************************************************/
AxedaCorp 0:65004368569c 104 /*net_socketOpen() */
AxedaCorp 0:65004368569c 105 /* */
AxedaCorp 0:65004368569c 106 /*This function opens a network port and connects to it. The connection point is defined by server*/
AxedaCorp 0:65004368569c 107 /*and port in the arguments. A reference to the port should be stored in the sock structure. If */
AxedaCorp 0:65004368569c 108 /*the secure flag is set to AX_TRUE your code should enable TLS or SSL at this stage. */
AxedaCorp 0:65004368569c 109 /* */
AxedaCorp 0:65004368569c 110 /*************************************************************************************************/
AxedaCorp 0:65004368569c 111 int net_socketOpen(ax_socket *sock, char *server, int port, int secure){
AxedaCorp 0:65004368569c 112 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 113
AxedaCorp 0:65004368569c 114 Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 115 // Mode mode=TCP;
AxedaCorp 0:65004368569c 116 bool result=wifi->open(server, port, Wifi::TCP);
AxedaCorp 0:65004368569c 117 // bool result=wifi->open("192.168.1.107", port, Wifi::TCP);
AxedaCorp 0:65004368569c 118 if(result) {
AxedaCorp 0:65004368569c 119 retVal=AX_OK;
AxedaCorp 0:65004368569c 120 }
AxedaCorp 0:65004368569c 121 else {retVal=AX_NET_ERR_UNABLE_TO_CONNECT; }
AxedaCorp 0:65004368569c 122
AxedaCorp 0:65004368569c 123 return retVal;
AxedaCorp 0:65004368569c 124 }
AxedaCorp 0:65004368569c 125 /*************************************************************************************************/
AxedaCorp 0:65004368569c 126 /*net_socketWrite() */
AxedaCorp 0:65004368569c 127 /* */
AxedaCorp 0:65004368569c 128 /*This function is used to write the data to a network port that has already been open and is */
AxedaCorp 0:65004368569c 129 /*connected. As usual the ax_socket struct should contain a reference to your port that you wish */
AxedaCorp 0:65004368569c 130 /*to write to. Data contains the data to be written and size describes how long the data is. */
AxedaCorp 0:65004368569c 131 /* */
AxedaCorp 0:65004368569c 132 /*************************************************************************************************/
AxedaCorp 0:65004368569c 133 int net_socketWrite(ax_socket *sock, char *data, int size) {
AxedaCorp 0:65004368569c 134 int retVal=AX_OK;
AxedaCorp 0:65004368569c 135 // Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 136 // int written= wifi->write(data, size, 300);
AxedaCorp 0:65004368569c 137 int fullsz=size+1;
AxedaCorp 0:65004368569c 138 if(buff_size==0) {
AxedaCorp 0:65004368569c 139 temp_buff=(char *)calloc(fullsz, sizeof(char));
AxedaCorp 0:65004368569c 140 int wrt=snprintf(temp_buff, fullsz, "%s", data);
AxedaCorp 0:65004368569c 141 buff_size++;
AxedaCorp 0:65004368569c 142 }
AxedaCorp 0:65004368569c 143 else {
AxedaCorp 0:65004368569c 144 int total=strlen(temp_buff)+fullsz;
AxedaCorp 0:65004368569c 145 char *temp=(char *)calloc(total, sizeof(char));
AxedaCorp 0:65004368569c 146 snprintf(temp, total, "%s%s", temp_buff, data);
AxedaCorp 0:65004368569c 147 free(temp_buff);
AxedaCorp 0:65004368569c 148 temp_buff=temp;
AxedaCorp 0:65004368569c 149 buff_size++;
AxedaCorp 0:65004368569c 150 }
AxedaCorp 0:65004368569c 151
AxedaCorp 0:65004368569c 152 return retVal;
AxedaCorp 0:65004368569c 153 }
AxedaCorp 0:65004368569c 154
AxedaCorp 0:65004368569c 155
AxedaCorp 0:65004368569c 156 /*************************************************************************************************/
AxedaCorp 0:65004368569c 157 /*net_socketFlush() */
AxedaCorp 0:65004368569c 158 /* */
AxedaCorp 0:65004368569c 159 /*This function will be called to handle writing all the data out to a network port. This function*/
AxedaCorp 0:65004368569c 160 /*may not be needed depending on your system. The single argument is a pointer to a struct that */
AxedaCorp 0:65004368569c 161 /*should contain the necessary references to the port on your local system. */
AxedaCorp 0:65004368569c 162 /* */
AxedaCorp 0:65004368569c 163 /*************************************************************************************************/
AxedaCorp 0:65004368569c 164 int net_socketFlush(ax_socket *sock){
AxedaCorp 0:65004368569c 165 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 166 int size=strlen(temp_buff);
AxedaCorp 0:65004368569c 167
AxedaCorp 0:65004368569c 168 Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 169 wifi->write(temp_buff, size, 300); //Wr
AxedaCorp 0:65004368569c 170
AxedaCorp 0:65004368569c 171 free(temp_buff);
AxedaCorp 0:65004368569c 172 buff_size=0;
AxedaCorp 0:65004368569c 173
AxedaCorp 0:65004368569c 174 return retVal;
AxedaCorp 0:65004368569c 175 }
AxedaCorp 0:65004368569c 176 /*************************************************************************************************/
AxedaCorp 0:65004368569c 177 /*net_socketClose() */
AxedaCorp 0:65004368569c 178 /* */
AxedaCorp 0:65004368569c 179 /*This function will be called to handle a socket close operation. It is used primarily by the */
AxedaCorp 0:65004368569c 180 /*axHTTP part of the library. The single argument is pointer to a structure that should contain */
AxedaCorp 0:65004368569c 181 /*the necessary references to the port on your local system. */
AxedaCorp 0:65004368569c 182 /*Override the source in this function to close a network port */
AxedaCorp 0:65004368569c 183 /*************************************************************************************************/
AxedaCorp 0:65004368569c 184 int net_socketClose(ax_socket *sock){
AxedaCorp 0:65004368569c 185 // int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 186 Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 187 wifi->close();
AxedaCorp 0:65004368569c 188 int ctr=30;
AxedaCorp 0:65004368569c 189
AxedaCorp 0:65004368569c 190 while((wifi->isOpen()==true)&&(ctr>0)) {
AxedaCorp 0:65004368569c 191 wifi->close();
AxedaCorp 0:65004368569c 192 printf("Socket Not closed, %d\n", ctr);
AxedaCorp 0:65004368569c 193 ctr--;
AxedaCorp 0:65004368569c 194 wait(1);
AxedaCorp 0:65004368569c 195 }
AxedaCorp 0:65004368569c 196
AxedaCorp 0:65004368569c 197 return AX_OK;
AxedaCorp 0:65004368569c 198 }
AxedaCorp 0:65004368569c 199
AxedaCorp 0:65004368569c 200 /*************************************************************************************************/
AxedaCorp 0:65004368569c 201 /*net_socketRead() */
AxedaCorp 0:65004368569c 202 /* */
AxedaCorp 0:65004368569c 203 /*This function is a wrapper function that will read returned data from a previous web call. It */
AxedaCorp 0:65004368569c 204 /*is assumed that any connection related to ax_socket will have already been established. The */
AxedaCorp 0:65004368569c 205 /*function should read the data and store it into the *data pointer, then populate the number of */
AxedaCorp 0:65004368569c 206 /*bytes read in the size variable. The axHTTP library will handle parsing and making sense of the*/
AxedaCorp 0:65004368569c 207 /*data that it is provided. The timeout will be passed as a parameter but is not implemented by */
AxedaCorp 0:65004368569c 208 /*any of the calling libraries, implement it here or pass to another local function if available.*/
AxedaCorp 0:65004368569c 209 /*************************************************************************************************/
AxedaCorp 0:65004368569c 210 int net_socketRead(ax_socket *sock, int timeout, unsigned char *buffer, int bufferSz, int *readSz){
AxedaCorp 0:65004368569c 211 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 212 Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 213 int bytesRead = wifi->read((char *)buffer, bufferSz, 300);
AxedaCorp 0:65004368569c 214 *readSz=bytesRead;
AxedaCorp 0:65004368569c 215 if(bytesRead>0) {return AX_OK; }
AxedaCorp 0:65004368569c 216
AxedaCorp 0:65004368569c 217 return retVal;
AxedaCorp 0:65004368569c 218 }
AxedaCorp 0:65004368569c 219
AxedaCorp 0:65004368569c 220 /*************************************************************************************************/
AxedaCorp 0:65004368569c 221 /*scm_download_req() */
AxedaCorp 0:65004368569c 222 /* */
AxedaCorp 0:65004368569c 223 /*This function will be called whenever an egress message is recieved with a file download command*/
AxedaCorp 0:65004368569c 224 /*You can write the code in the function to handle the file immediately OR store the structure for*/
AxedaCorp 0:65004368569c 225 /*later processing. The latter is preferable as this request may prevent the network port from */
AxedaCorp 0:65004368569c 226 /*being closed in a timely fashion. */
AxedaCorp 0:65004368569c 227 /* */
AxedaCorp 0:65004368569c 228 /*************************************************************************************************/
AxedaCorp 0:65004368569c 229 int scm_download_req(ax_package *request) {
AxedaCorp 0:65004368569c 230
AxedaCorp 0:65004368569c 231
AxedaCorp 0:65004368569c 232 }
AxedaCorp 0:65004368569c 233
AxedaCorp 0:65004368569c 234
AxedaCorp 0:65004368569c 235 /*************************************************************************************************/
AxedaCorp 0:65004368569c 236 /*scm_file_write() */
AxedaCorp 0:65004368569c 237 /* */
AxedaCorp 0:65004368569c 238 /*This function will be called whenever a file download is requested from the system. Override the*/
AxedaCorp 0:65004368569c 239 /*function to direct the output to serial or a file system or other storage device of your choice*/
AxedaCorp 0:65004368569c 240 /*The parameters to the function will be filled in when it's called by the toolkit */
AxedaCorp 0:65004368569c 241 /* */
AxedaCorp 0:65004368569c 242 /*char *filename: Just what it sounds like, the file name of the file */
AxedaCorp 0:65004368569c 243 /*char *data: The actual data of the file. */
AxedaCorp 0:65004368569c 244 /*int length: The number of bytes received */
AxedaCorp 0:65004368569c 245 /*ax_package_instruction *downloadInfo: a struct containing filename/path etc. */
AxedaCorp 0:65004368569c 246 /*************************************************************************************************/
AxedaCorp 0:65004368569c 247 int scm_file_write(char *filename, char *data, int length, ax_package_instruction *downloadInfo){
AxedaCorp 0:65004368569c 248
AxedaCorp 0:65004368569c 249
AxedaCorp 0:65004368569c 250 return AX_OK;
AxedaCorp 0:65004368569c 251 }
AxedaCorp 0:65004368569c 252
AxedaCorp 0:65004368569c 253
AxedaCorp 0:65004368569c 254
AxedaCorp 0:65004368569c 255 /*************************************************************************************************/
AxedaCorp 0:65004368569c 256 /*data_item_write() */
AxedaCorp 0:65004368569c 257 /* */
AxedaCorp 0:65004368569c 258 /*If a set data item command is recieved from the platform server this function will be called to*/
AxedaCorp 0:65004368569c 259 /*handle it each time. The function is here to allow for external data to be modified as needed. */
AxedaCorp 0:65004368569c 260 /*It could toggle a pin and light up an led or set a global variable in another thread. */
AxedaCorp 0:65004368569c 261 /* */
AxedaCorp 0:65004368569c 262 /*char *name: The name of the data item on the platform */
AxedaCorp 0:65004368569c 263 /*int ax_type: {AX_STRING|AX_ANALoG|AX_DIGITAL} indicates whether the sValue or dValue parameter */
AxedaCorp 0:65004368569c 264 /* will have the intended data. */
AxedaCorp 0:65004368569c 265 /*char *sValue: if ax_type==AX_STRING then this will be a pointer to a valid cstring */
AxedaCorp 0:65004368569c 266 /*int dValue: if ax_type==AX_ANLOG|AX_DIGITAL this value will be populated. It should be not be */
AxedaCorp 0:65004368569c 267 /* checked if the type is AX_STRING */
AxedaCorp 0:65004368569c 268 /*************************************************************************************************/
AxedaCorp 0:65004368569c 269 int data_item_write(char *name, char *sValue, int dValue, int ax_type) {
AxedaCorp 0:65004368569c 270
AxedaCorp 0:65004368569c 271 return AX_OK;
AxedaCorp 0:65004368569c 272 }
AxedaCorp 0:65004368569c 273
AxedaCorp 0:65004368569c 274 /*************************************************************************************************/
AxedaCorp 0:65004368569c 275 /*data_item_request() */
AxedaCorp 0:65004368569c 276 /* */
AxedaCorp 0:65004368569c 277 /*This function is called whenever the platform requests a data item's current value. Since this */
AxedaCorp 0:65004368569c 278 /*library is designed to run as it's own thread it does not maintain a list of data items and */
AxedaCorp 0:65004368569c 279 /*values. The implementation of this function should call an appropriate library or check an input*/
AxedaCorp 0:65004368569c 280 /*then create a dataItem set and send it immediately or on the next ping. */
AxedaCorp 0:65004368569c 281 /* */
AxedaCorp 0:65004368569c 282 /*char *name, the name of the data item being requested */
AxedaCorp 0:65004368569c 283 /* */
AxedaCorp 0:65004368569c 284 /*************************************************************************************************/
AxedaCorp 0:65004368569c 285 int data_item_request(char *name) {
AxedaCorp 0:65004368569c 286
AxedaCorp 0:65004368569c 287 return AX_OK;
AxedaCorp 0:65004368569c 288 }
AxedaCorp 0:65004368569c 289
AxedaCorp 0:65004368569c 290 /***************************************************************************************************/
AxedaCorp 0:65004368569c 291 /***********************************************{Custom helper methods}****************************/