sakura fan / SakuraIO_official

Fork of SakuraIO by SAKURA Internet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SakuraIO.cpp Source File

SakuraIO.cpp

00001 #include "mbed.h"
00002 #include "SakuraIO.h"
00003 #include "SakuraIO/commands.h"
00004 #include "SakuraIO/debug.h"
00005 
00006 uint8_t SakuraIO::executeCommand(uint8_t cmd,uint8_t requestLength, uint8_t *request, uint8_t *responseLength, uint8_t *response)
00007 {
00008   uint8_t parity = 0x00;
00009   uint8_t result = 0x00;
00010   uint8_t tmpResponseLength, tmpResponse;
00011 
00012   dbgln("executeCommand");
00013 
00014   this->begin();
00015 
00016   // request
00017   this->sendByte(cmd);
00018   this->sendByte(requestLength);
00019   parity = cmd ^ requestLength;
00020   for(int16_t i=0; i<requestLength; i++){
00021     parity ^= request[i];
00022     this->sendByte(request[i]);
00023   }
00024   this->sendByte(parity);
00025   //this->finishSending();
00026 
00027   tmpResponseLength = 0;
00028   if(responseLength != NULL){
00029     tmpResponseLength = *responseLength;
00030   }
00031 
00032   wait_ms(10);
00033 
00034   // response
00035   this->startReceive(tmpResponseLength+3);
00036   result = this->receiveByte();
00037   if(result != CMD_ERROR_NONE){
00038     dbgln("Invalid status");
00039     this->end();
00040     return result;
00041   }
00042   tmpResponseLength = this->receiveByte();
00043   parity = result ^ tmpResponseLength;
00044   if(responseLength != NULL){
00045     if(*responseLength < tmpResponseLength){
00046       tmpResponseLength = *responseLength;
00047     }
00048     *responseLength = tmpResponseLength;
00049   }
00050   for(int16_t i=0; i<tmpResponseLength; i++){
00051     tmpResponse = this->receiveByte();
00052     parity ^= tmpResponse;
00053     if(response != NULL){
00054       response[i] = tmpResponse;
00055     }
00056   }
00057   dbgln("Parity");
00058   uint8_t p = this->receiveByte(true);
00059   parity ^= p;
00060   dbg("Parity=");
00061   dbgln(p);
00062   if(parity != 0x00){
00063     result = CMD_ERROR_PARITY;
00064     dbgln("Invalid parity");
00065   }else{
00066     dbgln("Success");
00067   }
00068   //this->finishReceiving();
00069 
00070   this->end();
00071   return result;
00072 }
00073 
00074 /* Common Commands */
00075 
00076 uint8_t SakuraIO::getConnectionStatus(){
00077   uint8_t responseLength = 1;
00078   uint8_t response[1] = {0x00};
00079   if(executeCommand(CMD_GET_CONNECTION_STATUS, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
00080     return 0x7F;
00081   }
00082   return response[0];
00083 }
00084 
00085 uint8_t SakuraIO::getSignalQuarity(){
00086   uint8_t responseLength = 1;
00087   uint8_t response[1] = {0x00};
00088 
00089   if(executeCommand(CMD_GET_SIGNAL_QUALITY, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
00090     return 0x00;
00091   }
00092   return response[0];
00093 }
00094 
00095 uint64_t SakuraIO::getUnixtime(){
00096   uint8_t responseLength = 8;
00097   uint8_t response[8] = {0x00};
00098   if(executeCommand(CMD_GET_DATETIME, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
00099     return 0x00;
00100   }
00101   return *((uint64_t *)response);
00102 }
00103 
00104 uint8_t SakuraIO::echoback(uint8_t length, uint8_t *data, uint8_t *response){
00105   uint8_t responseLength = length;
00106   if(executeCommand(CMD_ECHO_BACK, length, data, &responseLength, response) != CMD_ERROR_NONE){
00107     return 0x00;
00108   }
00109   return responseLength;
00110 }
00111 
00112 /* IO Commands */
00113 
00114 uint16_t SakuraIO::getADC(uint8_t channel){
00115   uint8_t request[1] = {channel};
00116   uint8_t response[2] = {0x00};
00117   uint8_t responseLength = sizeof(response);
00118   if(executeCommand(CMD_READ_ADC, 1, request, &responseLength, response) != CMD_ERROR_NONE){
00119     return 0xffff;
00120   }
00121   return *((uint16_t *)response);
00122 }
00123 
00124 /* TX Commands */
00125 uint8_t SakuraIO::enqueueTxRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset){
00126   uint8_t request[18] = {0x00};
00127   uint8_t requestLength = 10;
00128   request[0] = ch;
00129   request[1] = type;
00130   for(uint8_t i=0;i<length;i++){
00131     request[2+i] = data[i];
00132   }
00133   if(offset != 0){
00134     requestLength = 18;
00135     for(uint8_t i=0;i<8;i++){
00136       request[10+i] = ((uint8_t *)&offset)[i];
00137     }
00138   }
00139   return executeCommand(CMD_TX_ENQUEUE, requestLength, request, NULL, NULL);
00140 }
00141 
00142 uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value, uint64_t offset){
00143   return enqueueTxRaw(ch, 'i', 4, (uint8_t *)&value, offset);
00144 }
00145 
00146 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value, uint64_t offset){
00147   return enqueueTxRaw(ch, 'I', 4, (uint8_t *)&value, offset);
00148 }
00149 
00150 uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value, uint64_t offset){
00151   return enqueueTxRaw(ch, 'l', 8, (uint8_t *)&value, offset);
00152 }
00153 
00154 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value, uint64_t offset){
00155   return enqueueTxRaw(ch, 'L', 8, (uint8_t *)&value, offset);
00156 }
00157 
00158 uint8_t SakuraIO::enqueueTx(uint8_t ch, float value, uint64_t offset){
00159   return enqueueTxRaw(ch, 'f', 4, (uint8_t *)&value, offset);
00160 }
00161 
00162 uint8_t SakuraIO::enqueueTx(uint8_t ch, double value, uint64_t offset){
00163   return enqueueTxRaw(ch, 'd', 8, (uint8_t *)&value, offset);
00164 }
00165 
00166 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8], uint64_t offset){
00167   return enqueueTxRaw(ch, 'b', 8, (uint8_t *)value, offset);
00168 }
00169 
00170 uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value){
00171   return enqueueTx(ch, value, (uint32_t)0);
00172 }
00173 
00174 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value){
00175   return enqueueTx(ch, value, (uint32_t)0);
00176 }
00177 
00178 uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value){
00179   return enqueueTx(ch, value, (uint32_t)0);
00180 }
00181 
00182 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value){
00183   return enqueueTx(ch, value, (uint32_t)0);
00184 }
00185 
00186 uint8_t SakuraIO::enqueueTx(uint8_t ch, float value){
00187   return enqueueTx(ch, value, (uint32_t)0);
00188 }
00189 
00190 uint8_t SakuraIO::enqueueTx(uint8_t ch, double value){
00191   return enqueueTx(ch, value, (uint32_t)0);
00192 }
00193 
00194 uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8]){
00195   return enqueueTx(ch, value, (uint32_t)0);
00196 }
00197 
00198 uint8_t SakuraIO::getTxQueueLength(uint8_t *available, uint8_t *queued){
00199   uint8_t response[2] = {0x00};
00200   uint8_t responseLength = 2;
00201   uint8_t ret = executeCommand(CMD_TX_LENGTH, 0, NULL, &responseLength, response);
00202   *available = response[0];
00203   *queued = response[1];
00204   return ret;
00205 }
00206 
00207 uint8_t SakuraIO::clearTx(){
00208   return executeCommand(CMD_TX_CLEAR, 0, NULL, NULL, NULL);
00209 }
00210 
00211 uint8_t SakuraIO::send(){
00212   return executeCommand(CMD_TX_SEND, 0, NULL, NULL, NULL);
00213 }
00214 
00215 uint8_t SakuraIO::getTxStatus(uint8_t *queue, uint8_t *immediate){
00216   uint8_t response[2] = {0x00};
00217   uint8_t responseLength = 2;
00218   uint8_t ret = executeCommand(CMD_TX_STAT, 0, NULL, &responseLength, response);
00219   *queue = response[0];
00220   *immediate = response[1];
00221   return ret;
00222 }
00223 
00224 /* RX Commands */
00225 
00226 uint8_t SakuraIO::dequeueRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){
00227   uint8_t response[18] = {0x00};
00228   uint8_t responseLength = 18;
00229   uint8_t ret = executeCommand(CMD_RX_DEQUEUE, 0, NULL, &responseLength, response);
00230   if(ret != CMD_ERROR_NONE){
00231     return ret;
00232   }
00233 
00234   *ch = response[0];
00235   *type = response[1];
00236   for(uint8_t i=0; i<8; i++){
00237     value[i] = response[2+i];
00238   }
00239   for(uint8_t i=0; i<8; i++){
00240     ((uint8_t *)offset)[i] = response[10+i];
00241   }
00242 
00243   return ret;
00244 }
00245 
00246 uint8_t SakuraIO::peekRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){
00247   uint8_t response[18] = {0x00};
00248   uint8_t responseLength = 18;
00249   uint8_t ret = executeCommand(CMD_RX_PEEK, 0, NULL, &responseLength, response);
00250   if(ret != CMD_ERROR_NONE){
00251     return ret;
00252   }
00253 
00254   *ch = response[0];
00255   *type = response[1];
00256   for(uint8_t i=0; i<8; i++){
00257     value[0] = response[2+i];
00258   }
00259   for(uint8_t i=0; i<8; i++){
00260     ((uint8_t *)offset)[i] = response[10+i];
00261   }
00262 
00263   return ret;
00264 }
00265 
00266 uint8_t SakuraIO::getRxQueueLength(uint8_t *available, uint8_t *queued){
00267   uint8_t response[2] = {0x00};
00268   uint8_t responseLength = 2;
00269   uint8_t ret = executeCommand(CMD_RX_LENGTH, 0, NULL, &responseLength, response);
00270   *available = response[0];
00271   *queued = response[1];
00272   return ret;
00273 }
00274 
00275 uint8_t SakuraIO::clearRx(){
00276   return executeCommand(CMD_RX_CLEAR, 0, NULL, NULL, NULL);
00277 }
00278 
00279 /* Operation command */
00280 
00281 uint16_t SakuraIO::getProductID(){
00282   uint8_t response[2] = {0x00};
00283   uint8_t responseLength = 2;
00284   uint8_t ret = executeCommand(CMD_GET_PRODUCT_ID, 0, NULL, &responseLength, response);
00285   if(ret != CMD_ERROR_NONE){
00286     return 0x00;
00287   }
00288   return *((uint16_t *)response);
00289 }
00290 
00291 uint8_t SakuraIO::getUniqueID(char *data){
00292   uint8_t response[11] = {0x00};
00293   uint8_t responseLength = 10;
00294   uint8_t ret = executeCommand(CMD_GET_UNIQUE_ID, 0, NULL, &responseLength, response);
00295   if(ret != CMD_ERROR_NONE){
00296     return ret;
00297   }
00298   for(uint8_t i=0; i<responseLength; i++){
00299     data[i] = (char)response[i];
00300   }
00301   data[responseLength] = 0x00;
00302   return ret;
00303 }
00304 
00305 uint8_t SakuraIO::getFirmwareVersion(char *data){
00306   uint8_t response[33] = {0x00};
00307   uint8_t responseLength = 32;
00308   uint8_t ret = executeCommand(CMD_GET_FIRMWARE_VERSION, 0, NULL, &responseLength, response);
00309   if(ret != CMD_ERROR_NONE){
00310     return ret;
00311   }
00312   for(uint8_t i=0; i<responseLength; i++){
00313     data[i] = (char)response[i];
00314   }
00315   data[responseLength] = 0x00;
00316   return ret;
00317 }
00318 
00319 uint8_t SakuraIO::unlock(){
00320   uint8_t request[4] = {0x53, 0x6B, 0x72, 0x61};
00321   return executeCommand(CMD_UNLOCK, 4, request, NULL, NULL);
00322 }
00323 
00324 uint8_t SakuraIO::updateFirmware(){
00325   return executeCommand(CMD_UPDATE_FIRMWARE, 0, 0, NULL, NULL);
00326 }
00327 
00328 uint8_t SakuraIO::getFirmwareUpdateStatus(){
00329   uint8_t response[1] = {0x00};
00330   uint8_t responseLength = 1;
00331   if(executeCommand(CMD_GET_UPDATE_FIRMWARE_STATUS, 0, 0, &responseLength, response) != CMD_ERROR_NONE){
00332       return 0xff;
00333   }
00334   return response[0];
00335 }
00336 
00337 uint8_t SakuraIO::reset(){
00338   return executeCommand(CMD_SOFTWARE_RESET, 0, 0, NULL, NULL);
00339 }