Xi Han / Hexi_KW40Z

Dependents:   Hexi_BLE_Example_Modified Hexi_BLE_Example_ModifiedPOTATO

Fork of Hexi_KW40Z by Hexiwear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hexi_KW40Z.cpp Source File

Hexi_KW40Z.cpp

00001 /** BLE KW40Z Driver for Hexiwear
00002  *  This file contains BLE and Touch Buttons driver functionality for Hexiwear
00003  *
00004  * Redistribution and use in source and binary forms, with or without modification,
00005  * are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this list
00008  * of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice, this
00011  * list of conditions and the following disclaimer in the documentation and/or
00012  * other materials provided with the distribution.
00013  *
00014  * Neither the name of NXP, nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  * visit: http://www.mikroe.com and http://www.nxp.com
00030  *
00031  * get support at: http://www.mikroe.com/forum and https://community.nxp.com
00032  *
00033  * Project HEXIWEAR, 2015
00034  */
00035  
00036 #include "Hexi_KW40Z.h"
00037  
00038 KW40Z::KW40Z(PinName txPin,PinName rxPin) :
00039 #if defined (LIB_DEBUG) || defined (RAW_DEBUG) || defined (RX_DEBUG)
00040 pc(USBTX, USBRX),
00041 #endif
00042 device(txPin, rxPin), rxThread(osPriorityNormal,1024), mainThread(osPriorityNormal,1024)
00043 {
00044 #if defined (LIB_DEBUG) || defined (RX_DEBUG)
00045     pc.baud(115200);
00046     printf("Initializing\r\n");
00047 #endif
00048 
00049     device.baud(230400);
00050     device.format(8, Serial::None, 2);
00051     
00052     rxBuff = (uint8_t*)&hostInterface_rxPacket;
00053     
00054     /* initialize callbacks */
00055     buttonUpCb = NULL;
00056     buttonDownCb = NULL;
00057     buttonLeftCb = NULL;
00058     buttonRightCb = NULL;
00059     buttonSlideCb = NULL;
00060     alertCb = NULL;
00061     messageCb = NULL;
00062     passkeyCb = NULL;
00063     notificationsCb = NULL;
00064 
00065     kw40_version.ver_patchNumber = 0;
00066     kw40_version.ver_minorNumber = 0;
00067     kw40_version.ver_majorNumber = 0;
00068     
00069     activeTsiGroup = 0;
00070     advertisementMode = 0;
00071     linkState = 0;
00072     bondPassKey = 0;
00073 
00074     allocated = 0;
00075     freed = 0;
00076     
00077     /* intialization finalized, signal to start the threads */
00078     rxThread.start(this, &KW40Z::rxTask);
00079     mainThread.start(this,&KW40Z::mainTask);
00080 }
00081 
00082 KW40Z::~KW40Z(void)
00083 {
00084 }
00085 
00086 void KW40Z::attach_buttonUp(button_t btnFct)    { buttonUpCb = btnFct;    }
00087 void KW40Z::attach_buttonDown(button_t btnFct)  { buttonDownCb = btnFct;  }
00088 void KW40Z::attach_buttonLeft(button_t btnFct)  { buttonLeftCb = btnFct;  }
00089 void KW40Z::attach_buttonRight(button_t btnFct) { buttonRightCb = btnFct; }
00090 void KW40Z::attach_buttonSlide(button_t btnFct) { buttonSlideCb = btnFct; }
00091 void KW40Z::attach_alert(alert_t alertFct)      { alertCb = alertFct;     }
00092 void KW40Z::attach_message(message_t msgFct)      { messageCb = msgFct;     }
00093 void KW40Z::attach_passkey(passkey_t passkeyFct){ passkeyCb = passkeyFct; }
00094 void KW40Z::attach_notifications(notifications_t notFct) { notificationsCb = notFct; }
00095 
00096 void KW40Z::mainTask(void)
00097 {
00098 #if defined (LIB_DEBUG)     
00099     printf("MainTask Stared\r\n");
00100 #endif
00101 
00102     SendGetActiveTsiGroup();
00103     SendGetAdvertisementMode();
00104     SendGetLinkState();
00105     SendGetVersion();
00106 
00107     while(1)
00108     {
00109         osEvent evt = queue.get();
00110         if (evt.status == osEventMessage) 
00111         {
00112             hostInterface_packet_t *packet = (hostInterface_packet_t*)evt.value.p;
00113             ProcessPacket(packet);
00114             mpool.free(packet);
00115             // freed += 1;
00116             // printf("%s%lu\r\n", "non-freed buffer slots: ", allocated - freed);
00117         }
00118         
00119     }
00120 }
00121 
00122 void KW40Z::rxTask(void)
00123 {
00124 #if defined (LIB_DEBUG) || defined (RX_DEBUG)   
00125     printf("RxTask Stared\r\n");
00126 #endif
00127 
00128     while(1)
00129     {
00130         if(device.readable()) 
00131         {   
00132             *rxBuff++ = device.getc();
00133             ProcessBuffer();
00134             
00135             /* check for buffer overflow */
00136             if(rxBuff >= ((uint8_t*)&hostInterface_rxPacket + sizeof(hostInterface_rxPacket)))
00137             {
00138                 rxBuff = (uint8_t*)&hostInterface_rxPacket;
00139             }
00140         }
00141     }
00142 }
00143 
00144 void KW40Z::SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested)
00145 {
00146     /* copy txPacket to the mem pool */
00147     hostInterface_packet_t *packet = mpool.alloc();
00148     
00149 
00150     if (packet == NULL) {
00151         return;
00152     }
00153     allocated += 1;
00154 
00155     memcpy(packet, txPacket, sizeof(hostInterface_packet_t));
00156     
00157     /* Set the TX bit in the Start 2 byte */
00158     packet->start2 |= gHostInterface_txPacketMask;
00159     
00160     if (true == confirmRequested)
00161     {
00162         /* Set the confirm requested bit in the Start 2 byte */
00163         packet->start2 |= gHostInterface_rxConfirmMask;
00164     }
00165     
00166 #if defined (LIB_DEBUG)
00167     printf("SendPacket: ");
00168     DebugPrintPacket(packet);
00169 #endif
00170     // printf("SendPacket: ");
00171     // DebugPrintPacket(packet);
00172 
00173     /* send message to main task */
00174     queue.put(packet);
00175 }
00176 
00177 void KW40Z::SendInternal(hostInterface_packet_t * txPacket)
00178 {
00179     uint8_t retries = 0;
00180     bool confirmRequested = false;
00181     confirmReceived = false;
00182     
00183 #if defined (LIB_DEBUG)
00184     printf("SendInternal: ");
00185     DebugPrintPacket(txPacket);
00186 #endif
00187     
00188     if(gHostInterface_rxConfirmMask == (txPacket->start2 & gHostInterface_rxConfirmMask))
00189     {
00190         confirmRequested = true;
00191 #if defined (LIB_DEBUG)
00192         printf("Found confirmRequested\r\n");
00193 #endif
00194     }
00195     
00196     do
00197     {
00198         char * txBuff = (char *)txPacket;
00199         uint8_t length = txPacket->length + gHostInterface_headerSize + 1;
00200         
00201         for(uint8_t i = 0; i < length; i++)
00202         {
00203             device.putc(*txBuff);
00204             txBuff++;
00205         }
00206         
00207 #if defined (LIB_DEBUG)
00208         printf("TX: ");
00209         DebugPrintPacket(txPacket);
00210 #endif
00211 
00212         retries++;
00213         
00214         #if defined (gHostInterface_RxConfirmationEnable)
00215         if((confirmRequested == true) && (confirmReceived == false))
00216         {
00217             Thread::wait(gHostInterface_retransmitTimeout);
00218         }
00219         #endif
00220     }
00221     while((confirmRequested == true) && 
00222           (confirmReceived == false) && 
00223           (retries < gHostInterface_retransmitCount));
00224 }
00225 
00226 void KW40Z::ProcessBuffer()
00227 {
00228 #if defined (RAW_DEBUG)
00229     printf("%02X ", rxBuff-1);
00230 #endif 
00231     /* check if header has been received */
00232     if(rxBuff > ((uint8_t*)&hostInterface_rxPacket + gHostInterface_headerSize))
00233     {
00234         /* check packet header */
00235         if((gHostInterface_startByte1 != hostInterface_rxPacket.start1)||
00236            (gHostInterface_startByte2 != (hostInterface_rxPacket.start2 & 0xFE))||
00237            (hostInterface_rxPacket.length > gHostInterface_dataSize))
00238         {
00239 #if defined (LIB_DEBUG) || defined (RX_DEBUG) 
00240             printf("Check header failed: ");
00241             DebugPrintPacket(&hostInterface_rxPacket);
00242 #endif
00243       
00244             SearchStartByte();
00245         }
00246         else
00247         {
00248             /* check data length */
00249             if(rxBuff > ((uint8_t*)&hostInterface_rxPacket + gHostInterface_headerSize + hostInterface_rxPacket.length)) 
00250             {
00251                 /* check trailer byte */
00252                 if(gHostInterface_trailerByte != hostInterface_rxPacket.data[hostInterface_rxPacket.length])
00253                 {
00254 #if defined (LIB_DEBUG) 
00255                     printf("Trailer byte failed: ");
00256                     DebugPrintPacket(&hostInterface_rxPacket);
00257 #endif
00258 
00259                     SearchStartByte();
00260                 }
00261                 else
00262                 {   
00263                 
00264                     #if defined (gHostInterface_RxConfirmationEnable)
00265                     if(hostInterface_rxPacket.type == packetType_OK)
00266                     {
00267                         confirmReceived = true;
00268                     }
00269                     #endif
00270                     
00271                     /* send message to main task */
00272                     hostInterface_packet_t *rxPacket = mpool.alloc();
00273                     allocated += 1;
00274                     memcpy(rxPacket, &hostInterface_rxPacket, sizeof(hostInterface_packet_t));
00275                     queue.put(rxPacket);
00276 
00277 #if defined (LIB_DEBUG)
00278                     printf("RX: ");
00279                     DebugPrintPacket(&hostInterface_rxPacket);
00280 #endif
00281 #if defined (RAW_DEBUG)
00282                     printf("\r\n");
00283 #endif
00284                     // printf("RX: ");
00285                     // DebugPrintPacket(&hostInterface_rxPacket);
00286                     // printf("\r\n");
00287                     /* reset buffer position */
00288                     rxBuff = (uint8_t*)&hostInterface_rxPacket;
00289                 } 
00290             }      
00291         }
00292     }
00293 }
00294 
00295 void KW40Z::SearchStartByte()
00296 {
00297     bool found = false;
00298     uint8_t * rdIdx = (uint8_t*)&hostInterface_rxPacket;
00299     rdIdx++;
00300         
00301     while(rdIdx < rxBuff)
00302     {
00303         if(*rdIdx == gHostInterface_startByte1)
00304         {
00305             uint32_t len = rxBuff - rdIdx + 1;
00306             
00307             memcpy(&hostInterface_rxPacket,rdIdx,len);
00308             rxBuff = (uint8_t*)&hostInterface_rxPacket + len;
00309             found = true;
00310             
00311 #if defined (LIB_DEBUG) 
00312             printf("moving %d", len);
00313 #endif
00314             break;
00315         }
00316         rdIdx++;
00317     }
00318     
00319     if(!found)
00320     {
00321         /* reset buffer position */
00322         rxBuff = (uint8_t*)&hostInterface_rxPacket;
00323         memset(rxBuff, 0, sizeof(hostInterface_packet_t));
00324     }
00325     
00326 #if defined (LIB_DEBUG) 
00327     printf("Search done: \r\n");
00328     printf("rxBuff: ");
00329     DebugPrintPacket((hostInterface_packet_t*)rxBuff);
00330     printf("rxPack: ");
00331     DebugPrintPacket(&hostInterface_rxPacket);
00332 #endif
00333 }
00334 
00335 void KW40Z::ProcessPacket(hostInterface_packet_t * packet)
00336 {
00337 #if defined (LIB_DEBUG)
00338     printf("ProcessPacket: ");
00339     DebugPrintPacket(packet);
00340 #endif
00341 
00342     /* check if this is a TX packet */
00343     if(gHostInterface_txPacketMask == (packet->start2 & gHostInterface_txPacketMask))
00344     {
00345         /* revert back the TX bit in Start2 byte */
00346         packet->start2 &= ~gHostInterface_txPacketMask;
00347         
00348         /* This is not a received packet, so call SendInternal */
00349         SendInternal(packet);
00350     }
00351     else
00352     {
00353 #ifdef gHostInterface_TxConfirmationEnable
00354         /* acknowledge the packet reception if requested by sender */
00355         if(gHostInterface_rxConfirmMask == (packet->start2 & gHostInterface_rxConfirmMask))
00356         {
00357           SendPacketOK();
00358         }
00359 #endif
00360 
00361         switch(packet->type)
00362         {
00363             /* button presses */
00364             case packetType_pressUp:
00365                 if(buttonUpCb != NULL) buttonUpCb();
00366                 break;
00367                 
00368             case packetType_pressDown:
00369                 if(buttonDownCb != NULL) buttonDownCb();
00370                 break;
00371                 
00372             case packetType_pressLeft:
00373                 if(buttonLeftCb != NULL) buttonLeftCb();
00374                 break;
00375                 
00376             case packetType_pressRight:
00377                 if(buttonRightCb != NULL) buttonRightCb();
00378                 break;
00379             
00380             case packetType_slide:
00381                 if(buttonSlideCb != NULL) buttonSlideCb();
00382                 break;
00383         
00384             /* Alert Service */
00385             case packetType_alertIn:
00386                 if(alertCb != NULL) alertCb(&packet->data[0], packet->length);
00387                 break;
00388                 
00389             /* Message Service */
00390             case packetType_messageIn:
00391                 if(messageCb != NULL)  messageCb(&packet->data[1], (uint8_t) packet->data[0]);
00392                 break;
00393             
00394             /* Passkey for pairing received */
00395             case packetType_passDisplay:
00396                 if(passkeyCb != NULL)
00397                 {
00398                     memcpy((uint8_t *)&bondPassKey,&packet->data[0], 3);
00399                     passkeyCb();
00400                 }
00401                 break;
00402                 
00403             /* OTAP messages */
00404             case packetType_otapCompleted:
00405             case packetType_otapFailed:
00406                 break;
00407                 
00408             /* TSI Status */
00409             case packetType_buttonsGroupSendActive:
00410                 activeTsiGroup = packet->data[0];
00411                 break;
00412                 
00413             /* Advertisement Mode Info */
00414             case packetType_advModeSend:
00415                 advertisementMode = packet->data[0];
00416                 break;
00417             
00418             /* Link State */
00419             case packetType_linkStateSend:
00420                 linkState = packet->data[0];
00421                 break;
00422                 
00423             /* ANCS Service Notification Received */
00424             case packetType_notification:
00425                 if(notificationsCb != NULL) notificationsCb(packet->data[0], packet->data[1]);
00426                 break;
00427                 
00428             /* Build version */
00429             case packetType_buildVersion:
00430                 kw40_version.ver_patchNumber = packet->data[2];
00431                 kw40_version.ver_minorNumber = packet->data[1];
00432                 kw40_version.ver_majorNumber = packet->data[0];
00433                 break;
00434           
00435             case packetType_OK:
00436                 /* do nothing, passthrough, the flag is set in the RxTask */
00437                 break;
00438                 
00439             default:
00440                 break;
00441         }
00442     }
00443 }
00444 
00445 void KW40Z::SendBatteryLevel(uint8_t percentage)
00446 {
00447     hostInterface_packet_t txPacket = {0};
00448     
00449     txPacket.start1     = gHostInterface_startByte1;
00450     txPacket.start2     = gHostInterface_startByte2;
00451     txPacket.type       = packetType_batteryLevel;
00452     txPacket.length     = 1;
00453     txPacket.data[0]    = percentage;
00454     txPacket.data[1]    = gHostInterface_trailerByte;
00455     
00456     SendPacket(&txPacket, true);
00457 }
00458 
00459 void KW40Z::SendAccel(int16_t x, int16_t y, int16_t z)
00460 {
00461     hostInterface_packet_t txPacket = {0};
00462     
00463     txPacket.start1     = gHostInterface_startByte1;
00464     txPacket.start2     = gHostInterface_startByte2;
00465     txPacket.type       = packetType_accel;
00466     txPacket.length     = 6;
00467         
00468     txPacket.data[0]    = (uint8_t) ((x >> 8)&0xFF); 
00469     txPacket.data[1]    = (uint8_t) x;
00470     txPacket.data[2]    = (uint8_t) ((y >> 8)&0xFF);    
00471     txPacket.data[3]    = (uint8_t) y;
00472     txPacket.data[4]    = (uint8_t) ((z >> 8)&0xFF);  
00473     txPacket.data[5]    = (uint8_t) z;
00474     txPacket.data[6]    = gHostInterface_trailerByte;
00475     
00476     SendPacket(&txPacket, true);
00477 }
00478 
00479 void KW40Z::SendGyro(int16_t x, int16_t y, int16_t z)
00480 {
00481     hostInterface_packet_t txPacket = {0};
00482     
00483     txPacket.start1     = gHostInterface_startByte1;
00484     txPacket.start2     = gHostInterface_startByte2;
00485     txPacket.type       = packetType_gyro;
00486     txPacket.length     = 6;   
00487     txPacket.data[0]    = (uint8_t) ((x >> 8)&0xFF); 
00488     txPacket.data[1]    = (uint8_t) x;
00489     txPacket.data[2]    = (uint8_t) ((y >> 8)&0xFF);    
00490     txPacket.data[3]    = (uint8_t) y;
00491     txPacket.data[4]    = (uint8_t) ((z >> 8)&0xFF);  
00492     txPacket.data[5]    = (uint8_t) z;
00493     txPacket.data[6]    = gHostInterface_trailerByte;
00494     
00495     SendPacket(&txPacket, true);
00496 }
00497 
00498 void KW40Z::SendMag(int16_t x, int16_t y, int16_t z)
00499 {
00500     hostInterface_packet_t txPacket = {0};
00501     
00502     txPacket.start1     = gHostInterface_startByte1;
00503     txPacket.start2     = gHostInterface_startByte2;
00504     txPacket.type       = packetType_magnet;
00505     txPacket.length     = 6;        
00506     txPacket.data[0]    = (uint8_t) ((x >> 8)&0xFF); 
00507     txPacket.data[1]    = (uint8_t) x;
00508     txPacket.data[2]    = (uint8_t) ((y >> 8)&0xFF);    
00509     txPacket.data[3]    = (uint8_t) y;
00510     txPacket.data[4]    = (uint8_t) ((z >> 8)&0xFF);  
00511     txPacket.data[5]    = (uint8_t) z;
00512     txPacket.data[6]    = gHostInterface_trailerByte;
00513     
00514     SendPacket(&txPacket, true);
00515 }
00516 
00517 void KW40Z::SendAmbientLight(uint8_t percentage)
00518 {
00519     hostInterface_packet_t txPacket = {0};
00520     
00521     txPacket.start1     = gHostInterface_startByte1;
00522     txPacket.start2     = gHostInterface_startByte2;
00523     txPacket.type       = packetType_ambiLight;
00524     txPacket.length     = 1;
00525     txPacket.data[0]    = percentage;
00526     txPacket.data[1]    = gHostInterface_trailerByte;
00527     
00528     SendPacket(&txPacket, true);
00529 }
00530 
00531 void KW40Z::SendTemperature(uint16_t celsius)
00532 {
00533     hostInterface_packet_t txPacket = {0};
00534     
00535     txPacket.start1     = gHostInterface_startByte1;
00536     txPacket.start2     = gHostInterface_startByte2;
00537     txPacket.type       = packetType_temperature;
00538     txPacket.length     = 2;
00539     memcpy(&txPacket.data[0],(uint8_t*)&celsius,txPacket.length);
00540     txPacket.data[2]    = gHostInterface_trailerByte;
00541     
00542     SendPacket(&txPacket, true);
00543 }
00544 
00545 void KW40Z::SendHumidity(uint16_t percentage)
00546 {
00547     hostInterface_packet_t txPacket = {0};
00548     
00549     txPacket.start1     = gHostInterface_startByte1;
00550     txPacket.start2     = gHostInterface_startByte2;
00551     txPacket.type       = packetType_humidity;
00552     txPacket.length     = 2;
00553     memcpy(&txPacket.data[0],(uint8_t*)&percentage,txPacket.length);
00554     txPacket.data[2]    = gHostInterface_trailerByte;
00555     
00556     SendPacket(&txPacket, true);
00557 }
00558 
00559 void KW40Z::SendPressure(uint16_t pascal)
00560 {
00561     hostInterface_packet_t txPacket = {0};
00562     
00563     txPacket.start1     = gHostInterface_startByte1;
00564     txPacket.start2     = gHostInterface_startByte2;
00565     txPacket.type       = packetType_pressure;
00566     txPacket.length     = 2;
00567     memcpy(&txPacket.data[0],(uint8_t*)&pascal,txPacket.length);
00568     txPacket.data[2]    = gHostInterface_trailerByte;
00569     
00570     SendPacket(&txPacket, true);
00571 }
00572 
00573 void KW40Z::SendHeartRate(uint8_t rate)
00574 {
00575     hostInterface_packet_t txPacket = {0};
00576     
00577     txPacket.start1     = gHostInterface_startByte1;
00578     txPacket.start2     = gHostInterface_startByte2;
00579     txPacket.type       = packetType_steps;
00580     txPacket.length     = 1;
00581     txPacket.data[0]    = rate;
00582     txPacket.data[1]    = gHostInterface_trailerByte;
00583     
00584     SendPacket(&txPacket, true);
00585 }
00586 
00587 void KW40Z::SendSteps(uint16_t steps)
00588 {
00589     hostInterface_packet_t txPacket = {0};
00590     
00591     txPacket.start1     = gHostInterface_startByte1;
00592     txPacket.start2     = gHostInterface_startByte2;
00593     txPacket.type       = packetType_steps;
00594     txPacket.length     = 2;
00595     memcpy(&txPacket.data[0],(uint8_t*)&steps,txPacket.length);
00596     txPacket.data[2]    = gHostInterface_trailerByte;
00597     
00598     SendPacket(&txPacket, true);
00599 }
00600 
00601 void KW40Z::SendCalories(uint16_t calories)
00602 {
00603     hostInterface_packet_t txPacket = {0};
00604     
00605     txPacket.start1     = gHostInterface_startByte1;
00606     txPacket.start2     = gHostInterface_startByte2;
00607     txPacket.type       = packetType_calories;
00608     txPacket.length     = 2;
00609     memcpy(&txPacket.data[0],(uint8_t*)&calories,txPacket.length);
00610     txPacket.data[2]    = gHostInterface_trailerByte;
00611     
00612     SendPacket(&txPacket, true);
00613 }
00614 
00615 void KW40Z::SendAlert(uint8_t *pData, uint8_t length)
00616 {
00617     hostInterface_packet_t txPacket = {0};
00618     
00619     txPacket.start1     = gHostInterface_startByte1;
00620     txPacket.start2     = gHostInterface_startByte2;
00621     txPacket.type       = packetType_alertOut;
00622     txPacket.length     = length;
00623     memcpy(&txPacket.data[0],pData,length);
00624     txPacket.data[length] = gHostInterface_trailerByte;
00625     
00626     SendPacket(&txPacket, true);
00627 }
00628 
00629 void KW40Z::SendMessage(uint8_t *pData, uint8_t length)
00630 {
00631     hostInterface_packet_t txPacket = {0};
00632     
00633     txPacket.start1     = gHostInterface_startByte1;
00634     txPacket.start2     = gHostInterface_startByte2;
00635     txPacket.type       = packetType_messageOut;
00636     txPacket.length     = 20;
00637     memcpy(&txPacket.data[1],pData,length);
00638     txPacket.data[0] = length;
00639     txPacket.data[20] = gHostInterface_trailerByte;
00640     
00641     SendPacket(&txPacket, true);
00642 }
00643 
00644 
00645 void KW40Z::ToggleTsiGroup(void)
00646 {
00647     hostInterface_packet_t txPacket = {0};
00648     
00649     txPacket.start1     = gHostInterface_startByte1;
00650     txPacket.start2     = gHostInterface_startByte2;
00651     txPacket.type       = packetType_buttonsGroupToggleActive;
00652     txPacket.length     = 0;
00653     txPacket.data[0]    = gHostInterface_trailerByte;
00654     
00655     SendPacket(&txPacket, true);
00656 }
00657 
00658 void KW40Z::ToggleAdvertisementMode(void)
00659 {
00660     hostInterface_packet_t txPacket = {0};
00661     
00662     txPacket.start1     = gHostInterface_startByte1;
00663     txPacket.start2     = gHostInterface_startByte2;
00664     txPacket.type       = packetType_advModeToggle;
00665     txPacket.length     = 0;
00666     txPacket.data[0]    = gHostInterface_trailerByte;
00667     
00668     SendPacket(&txPacket, true);
00669 }
00670 
00671 void KW40Z::SendSetApplicationMode(gui_current_app_t mode)
00672 {
00673     hostInterface_packet_t txPacket = {0};
00674     
00675     txPacket.start1     = gHostInterface_startByte1;
00676     txPacket.start2     = gHostInterface_startByte2;
00677     txPacket.type       = packetType_appMode;
00678     txPacket.length     = 1;
00679     txPacket.data[0]    = (uint8_t)mode;
00680     txPacket.data[1]    = gHostInterface_trailerByte;
00681     
00682     SendPacket(&txPacket, true);
00683 }
00684 
00685 void KW40Z::SendGetActiveTsiGroup(void)
00686 {
00687     hostInterface_packet_t txPacket = {0};
00688     
00689     txPacket.start1     = gHostInterface_startByte1;
00690     txPacket.start2     = gHostInterface_startByte2;
00691     txPacket.type       = packetType_buttonsGroupGetActive;
00692     txPacket.length     = 0;
00693     txPacket.data[0]    = gHostInterface_trailerByte;
00694     
00695     SendPacket(&txPacket, false);
00696 }
00697 
00698 void KW40Z::SendGetAdvertisementMode(void)
00699 {
00700     hostInterface_packet_t txPacket = {0};
00701     
00702     txPacket.start1     = gHostInterface_startByte1;
00703     txPacket.start2     = gHostInterface_startByte2;
00704     txPacket.type       = packetType_advModeGet;
00705     txPacket.length     = 0;
00706     txPacket.data[0]    = gHostInterface_trailerByte;
00707     
00708     SendPacket(&txPacket, false);
00709 }
00710 
00711 void KW40Z::SendGetLinkState(void)
00712 {
00713     hostInterface_packet_t txPacket = {0};
00714     
00715     txPacket.start1     = gHostInterface_startByte1;
00716     txPacket.start2     = gHostInterface_startByte2;
00717     txPacket.type       = packetType_linkStateGet;
00718     txPacket.length     = 0;
00719     txPacket.data[0]    = gHostInterface_trailerByte;
00720     
00721     SendPacket(&txPacket, false);
00722 }
00723 
00724 void KW40Z::SendGetVersion(void)
00725 {
00726     hostInterface_packet_t txPacket = {0};
00727     
00728     txPacket.start1     = gHostInterface_startByte1;
00729     txPacket.start2     = gHostInterface_startByte2;
00730     txPacket.type       = packetType_buildVersion;
00731     txPacket.length     = 3;
00732     txPacket.data[0]    = HEXIWEAR_VERSION_MAJOR;
00733     txPacket.data[1]    = HEXIWEAR_VERSION_MINOR;
00734     txPacket.data[2]    = HEXIWEAR_VERSION_PATCH;
00735     txPacket.data[3]    = gHostInterface_trailerByte;
00736     
00737     SendPacket(&txPacket, true);
00738 }
00739 
00740 void KW40Z::SendPacketOK(void)
00741 {
00742     hostInterface_packet_t txPacket = {0};
00743     
00744     txPacket.start1     = gHostInterface_startByte1;
00745     txPacket.start2     = gHostInterface_startByte2;
00746     txPacket.type       = packetType_OK;
00747     txPacket.length     = 0;
00748     txPacket.data[0]    = gHostInterface_trailerByte;
00749     
00750     SendPacket(&txPacket, false);    
00751 }
00752 
00753 uint8_t KW40Z::GetAdvertisementMode(void)
00754 {
00755     return advertisementMode;
00756 }
00757 
00758 uint32_t KW40Z::GetPassKey(void)
00759 {
00760     return bondPassKey;
00761 }
00762    
00763 uint8_t KW40Z::GetLinkState(void)
00764 {
00765     return linkState;
00766 }
00767 
00768 hexiwear_version_t KW40Z::GetVersion(void)
00769 {
00770     return kw40_version;
00771 }
00772 
00773 uint8_t KW40Z::GetTsiGroup(void)
00774 {
00775     return activeTsiGroup;
00776 }
00777 
00778 #if defined (LIB_DEBUG) || defined (RX_DEBUG)
00779 void KW40Z::DebugPrintPacket(hostInterface_packet_t * packet)
00780 {
00781     char * idx = (char *)packet;
00782     uint8_t length = packet->length + gHostInterface_headerSize + 1;
00783     
00784     if(length > sizeof(hostInterface_packet_t))
00785     {
00786         length = sizeof(hostInterface_packet_t);
00787     }
00788 
00789     for(uint8_t i = 0; i < length; i++)
00790     {
00791         printf("%02X ",*idx);
00792         idx++;
00793     }
00794     printf("\r\n");
00795 }
00796 #endif
00797 // void KW40Z::DebugPrintPacket(hostInterface_packet_t * packet)
00798 // {
00799 //     char * idx = (char *)packet;
00800 //     uint8_t length = packet->length + gHostInterface_headerSize + 1;
00801     
00802 //     if(length > sizeof(hostInterface_packet_t))
00803 //     {
00804 //         length = sizeof(hostInterface_packet_t);
00805 //     }
00806 
00807 //     for(uint8_t i = 0; i < length; i++)
00808 //     {
00809 //         printf("%02X ",*idx);
00810 //         idx++;
00811 //     }
00812 //     printf("\r\n");
00813 // }