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.
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z by
axTransport.cpp
00001 #include "axTransport.h" 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <string.h> 00005 00006 #include "axStatusCodes.h" 00007 #include "axConstants.h" 00008 #include <math.h> 00009 #include "include_me.h" 00010 #include "mbed.h" 00011 00012 using namespace mts; 00013 char *temp_buff; 00014 int buff_size; 00015 00016 /************************************************************************************************/ 00017 /*getEpoch() */ 00018 /* */ 00019 /*This method should return the number of seconds since January 1st 1970. */ 00020 /* */ 00021 /*returns long value in seconds */ 00022 /************************************************************************************************/ 00023 long getEpoch(){ 00024 return (long)0; 00025 } 00026 00027 00028 /**************************************************************************************************/ 00029 /*ax_print() */ 00030 /* */ 00031 /*A wrapper method that will allow you to direct the output to a particular stream. Most of the */ 00032 /*time the output will just go to printf/std. out. This is called by the axAgent library to print*/ 00033 /*error and debug messages. It is reccomended to include processing for the integers AX_DEBUG_MSG*/ 00034 /*and AX_ERROR_MSG as the library will frequently use these options if configured. */ 00035 /* */ 00036 /* msgType: allows for an integer value to control how the data is displayed or written */ 00037 /* msg: a pointer to a string containing the data to be printed */ 00038 /************************************************************************************************/ 00039 void ax_print(int msgType, char *msg){ 00040 switch(msgType) { 00041 case AX_DEBUG_MSG: 00042 printf("DEBUG: %s\n", msg); 00043 break; 00044 case AX_ERROR_MSG: 00045 printf("ERR: %s\n", msg); 00046 break; 00047 } 00048 00049 } 00050 /************************************************************************************************/ 00051 /*sys_delay() */ 00052 /* */ 00053 /*A wrapper method that is meant to induce a delay on the system that it running it. Used mostly*/ 00054 /*to implement timeouts. */ 00055 /* */ 00056 /* */ 00057 /* */ 00058 /************************************************************************************************/ 00059 void sys_delay(int seconds){ 00060 00061 } 00062 00063 /************************************************************************************************/ 00064 /*randInt() */ 00065 /* */ 00066 /*This function is an abstraction function and should be populated with a call to the host's */ 00067 /*equivalent of the random function. */ 00068 /************************************************************************************************/ 00069 int randInt() { 00070 //call to the local create random method. 00071 srand(1235); 00072 return rand(); 00073 } 00074 00075 /************************************************************************************************/ 00076 /*seedRand() */ 00077 /* */ 00078 /*This function is an abstraction function and should be populated with a call to the host's */ 00079 /*equivalent of the function that seeds the random number generator */ 00080 /************************************************************************************************/ 00081 int seedRand(){ 00082 00083 00084 } 00085 00086 /*************************************************************************************************/ 00087 /*net_socketInit() */ 00088 /* */ 00089 /*This method provides a convenient way to call code that initializes the socket before it is */ 00090 /*actually called to open. For example, it could initialize a holder object that you code or set */ 00091 /*options on the socket itself. The return will be a code that could be passed up through the */ 00092 /*HTTP library and to the error stream. */ 00093 /*************************************************************************************************/ 00094 int net_socketInit(ax_socket *sock) { 00095 int retVal=AX_UNKNOWN; 00096 00097 buff_size=0; 00098 temp_buff=NULL; 00099 00100 return retVal; 00101 } 00102 00103 /*************************************************************************************************/ 00104 /*net_socketOpen() */ 00105 /* */ 00106 /*This function opens a network port and connects to it. The connection point is defined by server*/ 00107 /*and port in the arguments. A reference to the port should be stored in the sock structure. If */ 00108 /*the secure flag is set to AX_TRUE your code should enable TLS or SSL at this stage. */ 00109 /* */ 00110 /*************************************************************************************************/ 00111 int net_socketOpen(ax_socket *sock, char *server, int port, int secure){ 00112 int retVal=AX_UNKNOWN; 00113 00114 Wifi* wifi = Wifi::getInstance(); 00115 // Mode mode=TCP; 00116 bool result=wifi->open(server, port, Wifi::TCP); 00117 // bool result=wifi->open("192.168.1.107", port, Wifi::TCP); 00118 if(result) { 00119 retVal=AX_OK; 00120 } 00121 else {retVal=AX_NET_ERR_UNABLE_TO_CONNECT; } 00122 00123 return retVal; 00124 } 00125 /*************************************************************************************************/ 00126 /*net_socketWrite() */ 00127 /* */ 00128 /*This function is used to write the data to a network port that has already been open and is */ 00129 /*connected. As usual the ax_socket struct should contain a reference to your port that you wish */ 00130 /*to write to. Data contains the data to be written and size describes how long the data is. */ 00131 /* */ 00132 /*************************************************************************************************/ 00133 int net_socketWrite(ax_socket *sock, char *data, int size) { 00134 int retVal=AX_OK; 00135 // Wifi* wifi = Wifi::getInstance(); 00136 // int written= wifi->write(data, size, 300); 00137 int fullsz=size+1; 00138 if(buff_size==0) { 00139 temp_buff=(char *)calloc(fullsz, sizeof(char)); 00140 int wrt=snprintf(temp_buff, fullsz, "%s", data); 00141 buff_size++; 00142 } 00143 else { 00144 int total=strlen(temp_buff)+fullsz; 00145 char *temp=(char *)calloc(total, sizeof(char)); 00146 snprintf(temp, total, "%s%s", temp_buff, data); 00147 free(temp_buff); 00148 temp_buff=temp; 00149 buff_size++; 00150 } 00151 00152 return retVal; 00153 } 00154 00155 00156 /*************************************************************************************************/ 00157 /*net_socketFlush() */ 00158 /* */ 00159 /*This function will be called to handle writing all the data out to a network port. This function*/ 00160 /*may not be needed depending on your system. The single argument is a pointer to a struct that */ 00161 /*should contain the necessary references to the port on your local system. */ 00162 /* */ 00163 /*************************************************************************************************/ 00164 int net_socketFlush(ax_socket *sock){ 00165 int retVal=AX_UNKNOWN; 00166 int size=strlen(temp_buff); 00167 00168 Wifi* wifi = Wifi::getInstance(); 00169 wifi->write(temp_buff, size, 300); //Wr 00170 00171 free(temp_buff); 00172 buff_size=0; 00173 00174 return retVal; 00175 } 00176 /*************************************************************************************************/ 00177 /*net_socketClose() */ 00178 /* */ 00179 /*This function will be called to handle a socket close operation. It is used primarily by the */ 00180 /*axHTTP part of the library. The single argument is pointer to a structure that should contain */ 00181 /*the necessary references to the port on your local system. */ 00182 /*Override the source in this function to close a network port */ 00183 /*************************************************************************************************/ 00184 int net_socketClose(ax_socket *sock){ 00185 // int retVal=AX_UNKNOWN; 00186 Wifi* wifi = Wifi::getInstance(); 00187 wifi->close(); 00188 int ctr=30; 00189 00190 while((wifi->isOpen()==true)&&(ctr>0)) { 00191 wifi->close(); 00192 printf("Socket Not closed, %d\n", ctr); 00193 ctr--; 00194 wait(1); 00195 } 00196 00197 return AX_OK; 00198 } 00199 00200 /*************************************************************************************************/ 00201 /*net_socketRead() */ 00202 /* */ 00203 /*This function is a wrapper function that will read returned data from a previous web call. It */ 00204 /*is assumed that any connection related to ax_socket will have already been established. The */ 00205 /*function should read the data and store it into the *data pointer, then populate the number of */ 00206 /*bytes read in the size variable. The axHTTP library will handle parsing and making sense of the*/ 00207 /*data that it is provided. The timeout will be passed as a parameter but is not implemented by */ 00208 /*any of the calling libraries, implement it here or pass to another local function if available.*/ 00209 /*************************************************************************************************/ 00210 int net_socketRead(ax_socket *sock, int timeout, unsigned char *buffer, int bufferSz, int *readSz){ 00211 int retVal=AX_UNKNOWN; 00212 Wifi* wifi = Wifi::getInstance(); 00213 int bytesRead = wifi->read((char *)buffer, bufferSz, 300); 00214 *readSz=bytesRead; 00215 if(bytesRead>0) {return AX_OK; } 00216 00217 return retVal; 00218 } 00219 00220 /*************************************************************************************************/ 00221 /*scm_download_req() */ 00222 /* */ 00223 /*This function will be called whenever an egress message is recieved with a file download command*/ 00224 /*You can write the code in the function to handle the file immediately OR store the structure for*/ 00225 /*later processing. The latter is preferable as this request may prevent the network port from */ 00226 /*being closed in a timely fashion. */ 00227 /* */ 00228 /*************************************************************************************************/ 00229 int scm_download_req(ax_package *request) { 00230 00231 00232 } 00233 00234 00235 /*************************************************************************************************/ 00236 /*scm_file_write() */ 00237 /* */ 00238 /*This function will be called whenever a file download is requested from the system. Override the*/ 00239 /*function to direct the output to serial or a file system or other storage device of your choice*/ 00240 /*The parameters to the function will be filled in when it's called by the toolkit */ 00241 /* */ 00242 /*char *filename: Just what it sounds like, the file name of the file */ 00243 /*char *data: The actual data of the file. */ 00244 /*int length: The number of bytes received */ 00245 /*ax_package_instruction *downloadInfo: a struct containing filename/path etc. */ 00246 /*************************************************************************************************/ 00247 int scm_file_write(char *filename, char *data, int length, ax_package_instruction *downloadInfo){ 00248 00249 00250 return AX_OK; 00251 } 00252 00253 00254 00255 /*************************************************************************************************/ 00256 /*data_item_write() */ 00257 /* */ 00258 /*If a set data item command is recieved from the platform server this function will be called to*/ 00259 /*handle it each time. The function is here to allow for external data to be modified as needed. */ 00260 /*It could toggle a pin and light up an led or set a global variable in another thread. */ 00261 /* */ 00262 /*char *name: The name of the data item on the platform */ 00263 /*int ax_type: {AX_STRING|AX_ANALoG|AX_DIGITAL} indicates whether the sValue or dValue parameter */ 00264 /* will have the intended data. */ 00265 /*char *sValue: if ax_type==AX_STRING then this will be a pointer to a valid cstring */ 00266 /*int dValue: if ax_type==AX_ANLOG|AX_DIGITAL this value will be populated. It should be not be */ 00267 /* checked if the type is AX_STRING */ 00268 /*************************************************************************************************/ 00269 int data_item_write(char *name, char *sValue, int dValue, int ax_type) { 00270 00271 return AX_OK; 00272 } 00273 00274 /*************************************************************************************************/ 00275 /*data_item_request() */ 00276 /* */ 00277 /*This function is called whenever the platform requests a data item's current value. Since this */ 00278 /*library is designed to run as it's own thread it does not maintain a list of data items and */ 00279 /*values. The implementation of this function should call an appropriate library or check an input*/ 00280 /*then create a dataItem set and send it immediately or on the next ping. */ 00281 /* */ 00282 /*char *name, the name of the data item being requested */ 00283 /* */ 00284 /*************************************************************************************************/ 00285 int data_item_request(char *name) { 00286 00287 return AX_OK; 00288 } 00289 00290 /***************************************************************************************************/ 00291 /***********************************************{Custom helper methods}****************************/
Generated on Wed Jul 13 2022 03:01:22 by
