Hexiwear library for communicating with the on-board KW40Z BLE device. KW40Z handles also the touch buttons.
Fork of Hexi_KW40Z by
Hexi_KW40Z.cpp@1:f6f9b24aea57, 2016-09-19 (annotated)
- Committer:
- cotigac
- Date:
- Mon Sep 19 03:39:36 2016 +0000
- Revision:
- 1:f6f9b24aea57
- Parent:
- 0:c2d52562f36b
- Child:
- 2:bb66c19c3c04
Updated library for simpler callbacks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cotigac | 0:c2d52562f36b | 1 | /** BLE KW40Z Driver for Hexiwear |
cotigac | 0:c2d52562f36b | 2 | * This file contains BLE and Touch Buttons driver functionality for Hexiwear |
cotigac | 0:c2d52562f36b | 3 | * |
cotigac | 0:c2d52562f36b | 4 | * Redistribution and use in source and binary forms, with or without modification, |
cotigac | 0:c2d52562f36b | 5 | * are permitted provided that the following conditions are met: |
cotigac | 0:c2d52562f36b | 6 | * |
cotigac | 0:c2d52562f36b | 7 | * Redistributions of source code must retain the above copyright notice, this list |
cotigac | 0:c2d52562f36b | 8 | * of conditions and the following disclaimer. |
cotigac | 0:c2d52562f36b | 9 | * |
cotigac | 0:c2d52562f36b | 10 | * Redistributions in binary form must reproduce the above copyright notice, this |
cotigac | 0:c2d52562f36b | 11 | * list of conditions and the following disclaimer in the documentation and/or |
cotigac | 0:c2d52562f36b | 12 | * other materials provided with the distribution. |
cotigac | 0:c2d52562f36b | 13 | * |
cotigac | 0:c2d52562f36b | 14 | * Neither the name of NXP, nor the names of its |
cotigac | 0:c2d52562f36b | 15 | * contributors may be used to endorse or promote products derived from this |
cotigac | 0:c2d52562f36b | 16 | * software without specific prior written permission. |
cotigac | 0:c2d52562f36b | 17 | * |
cotigac | 0:c2d52562f36b | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
cotigac | 0:c2d52562f36b | 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
cotigac | 0:c2d52562f36b | 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cotigac | 0:c2d52562f36b | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
cotigac | 0:c2d52562f36b | 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
cotigac | 0:c2d52562f36b | 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
cotigac | 0:c2d52562f36b | 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
cotigac | 0:c2d52562f36b | 25 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
cotigac | 0:c2d52562f36b | 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
cotigac | 0:c2d52562f36b | 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cotigac | 0:c2d52562f36b | 28 | * |
cotigac | 0:c2d52562f36b | 29 | * visit: http://www.mikroe.com and http://www.nxp.com |
cotigac | 0:c2d52562f36b | 30 | * |
cotigac | 0:c2d52562f36b | 31 | * get support at: http://www.mikroe.com/forum and https://community.nxp.com |
cotigac | 0:c2d52562f36b | 32 | * |
cotigac | 0:c2d52562f36b | 33 | * Project HEXIWEAR, 2015 |
cotigac | 0:c2d52562f36b | 34 | */ |
cotigac | 0:c2d52562f36b | 35 | |
cotigac | 0:c2d52562f36b | 36 | #include "Hexi_KW40Z.h" |
cotigac | 0:c2d52562f36b | 37 | |
cotigac | 0:c2d52562f36b | 38 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 39 | RawSerial pc(USBTX, USBRX); // tx, rx |
cotigac | 0:c2d52562f36b | 40 | #endif |
cotigac | 0:c2d52562f36b | 41 | |
cotigac | 0:c2d52562f36b | 42 | KW40Z::KW40Z(PinName txPin,PinName rxPin) : device(txPin, rxPin), mainThread(&KW40Z::mainStarter, this, osPriorityNormal,1024), rxThread(&KW40Z::rxStarter, this, osPriorityNormal,1024) |
cotigac | 0:c2d52562f36b | 43 | { |
cotigac | 0:c2d52562f36b | 44 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 45 | pc.baud(115200); |
cotigac | 0:c2d52562f36b | 46 | pc.printf("Initializing\r\n"); |
cotigac | 0:c2d52562f36b | 47 | #endif |
cotigac | 0:c2d52562f36b | 48 | |
cotigac | 0:c2d52562f36b | 49 | device.baud(230400); |
cotigac | 0:c2d52562f36b | 50 | device.format(8, Serial::None, 2); |
cotigac | 0:c2d52562f36b | 51 | |
cotigac | 0:c2d52562f36b | 52 | rxBuff = (uint8_t*)&hostInterface_rxPacket; |
cotigac | 1:f6f9b24aea57 | 53 | |
cotigac | 1:f6f9b24aea57 | 54 | /* initialize callbacks */ |
cotigac | 1:f6f9b24aea57 | 55 | buttonUpCb = NULL; |
cotigac | 1:f6f9b24aea57 | 56 | buttonDownCb = NULL; |
cotigac | 1:f6f9b24aea57 | 57 | buttonLeftCb = NULL; |
cotigac | 1:f6f9b24aea57 | 58 | buttonRightCb = NULL; |
cotigac | 1:f6f9b24aea57 | 59 | buttonSlideCb = NULL; |
cotigac | 1:f6f9b24aea57 | 60 | alertCb = NULL; |
cotigac | 1:f6f9b24aea57 | 61 | passkeyCb = NULL; |
cotigac | 1:f6f9b24aea57 | 62 | notificationsCb = NULL; |
cotigac | 0:c2d52562f36b | 63 | |
cotigac | 0:c2d52562f36b | 64 | /* intialization finalized, signal to start the threads */ |
cotigac | 0:c2d52562f36b | 65 | mainThread.signal_set(START_THREAD); |
cotigac | 0:c2d52562f36b | 66 | rxThread.signal_set(START_THREAD); |
cotigac | 0:c2d52562f36b | 67 | } |
cotigac | 0:c2d52562f36b | 68 | |
cotigac | 0:c2d52562f36b | 69 | KW40Z::~KW40Z(void) |
cotigac | 0:c2d52562f36b | 70 | { |
cotigac | 0:c2d52562f36b | 71 | } |
cotigac | 0:c2d52562f36b | 72 | |
cotigac | 1:f6f9b24aea57 | 73 | void KW40Z::attach_buttonUp(button_t btnFct) |
cotigac | 1:f6f9b24aea57 | 74 | { |
cotigac | 1:f6f9b24aea57 | 75 | buttonUpCb = btnFct; |
cotigac | 1:f6f9b24aea57 | 76 | } |
cotigac | 1:f6f9b24aea57 | 77 | |
cotigac | 1:f6f9b24aea57 | 78 | void KW40Z::attach_buttonDown(button_t btnFct) |
cotigac | 1:f6f9b24aea57 | 79 | { |
cotigac | 1:f6f9b24aea57 | 80 | buttonDownCb = btnFct; |
cotigac | 1:f6f9b24aea57 | 81 | } |
cotigac | 1:f6f9b24aea57 | 82 | |
cotigac | 1:f6f9b24aea57 | 83 | void KW40Z::attach_buttonLeft(button_t btnFct) |
cotigac | 1:f6f9b24aea57 | 84 | { |
cotigac | 1:f6f9b24aea57 | 85 | buttonLeftCb = btnFct; |
cotigac | 1:f6f9b24aea57 | 86 | } |
cotigac | 1:f6f9b24aea57 | 87 | |
cotigac | 1:f6f9b24aea57 | 88 | void KW40Z::attach_buttonRight(button_t btnFct) |
cotigac | 0:c2d52562f36b | 89 | { |
cotigac | 1:f6f9b24aea57 | 90 | buttonRightCb = btnFct; |
cotigac | 1:f6f9b24aea57 | 91 | } |
cotigac | 1:f6f9b24aea57 | 92 | |
cotigac | 1:f6f9b24aea57 | 93 | void KW40Z::attach_buttonSlide(button_t btnFct) |
cotigac | 1:f6f9b24aea57 | 94 | { |
cotigac | 1:f6f9b24aea57 | 95 | buttonSlideCb = btnFct; |
cotigac | 1:f6f9b24aea57 | 96 | } |
cotigac | 1:f6f9b24aea57 | 97 | |
cotigac | 1:f6f9b24aea57 | 98 | void KW40Z::attach_alert(alert_t alertFct) |
cotigac | 1:f6f9b24aea57 | 99 | { |
cotigac | 1:f6f9b24aea57 | 100 | alertCb = alertFct; |
cotigac | 1:f6f9b24aea57 | 101 | } |
cotigac | 1:f6f9b24aea57 | 102 | |
cotigac | 1:f6f9b24aea57 | 103 | void KW40Z::attach_passkey(passkey_t passkeyFct) |
cotigac | 1:f6f9b24aea57 | 104 | { |
cotigac | 1:f6f9b24aea57 | 105 | passkeyCb = passkeyFct; |
cotigac | 1:f6f9b24aea57 | 106 | } |
cotigac | 1:f6f9b24aea57 | 107 | |
cotigac | 1:f6f9b24aea57 | 108 | void KW40Z::attach_notifications(notifications_t notFct) |
cotigac | 1:f6f9b24aea57 | 109 | { |
cotigac | 1:f6f9b24aea57 | 110 | notificationsCb = notFct; |
cotigac | 0:c2d52562f36b | 111 | } |
cotigac | 0:c2d52562f36b | 112 | |
cotigac | 0:c2d52562f36b | 113 | void KW40Z::rxStarter(void const *p) { |
cotigac | 0:c2d52562f36b | 114 | KW40Z *instance = (KW40Z*)p; |
cotigac | 0:c2d52562f36b | 115 | instance->rxTask(); |
cotigac | 0:c2d52562f36b | 116 | } |
cotigac | 0:c2d52562f36b | 117 | |
cotigac | 0:c2d52562f36b | 118 | void KW40Z::mainStarter(void const *p) { |
cotigac | 0:c2d52562f36b | 119 | KW40Z *instance = (KW40Z*)p; |
cotigac | 0:c2d52562f36b | 120 | instance->mainTask(); |
cotigac | 0:c2d52562f36b | 121 | } |
cotigac | 0:c2d52562f36b | 122 | |
cotigac | 0:c2d52562f36b | 123 | void KW40Z::GetVersion() |
cotigac | 0:c2d52562f36b | 124 | { |
cotigac | 0:c2d52562f36b | 125 | hostInterface_packet_t txPacket = {0}; |
cotigac | 0:c2d52562f36b | 126 | |
cotigac | 0:c2d52562f36b | 127 | txPacket.start1 = gHostInterface_startByte1, |
cotigac | 0:c2d52562f36b | 128 | txPacket.start2 = gHostInterface_startByte2, |
cotigac | 0:c2d52562f36b | 129 | txPacket.type = packetType_buildVersion, |
cotigac | 0:c2d52562f36b | 130 | txPacket.length = 3, |
cotigac | 0:c2d52562f36b | 131 | txPacket.data[0] = HEXIWEAR_VERSION_MAJOR; |
cotigac | 0:c2d52562f36b | 132 | txPacket.data[1] = HEXIWEAR_VERSION_MINOR; |
cotigac | 0:c2d52562f36b | 133 | txPacket.data[2] = HEXIWEAR_VERSION_PATCH; |
cotigac | 0:c2d52562f36b | 134 | txPacket.data[3] = gHostInterface_trailerByte; |
cotigac | 0:c2d52562f36b | 135 | |
cotigac | 0:c2d52562f36b | 136 | SendPacket(&txPacket, false); |
cotigac | 0:c2d52562f36b | 137 | } |
cotigac | 0:c2d52562f36b | 138 | |
cotigac | 0:c2d52562f36b | 139 | void KW40Z::SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested) |
cotigac | 0:c2d52562f36b | 140 | { |
cotigac | 0:c2d52562f36b | 141 | uint8_t retries = 0; |
cotigac | 0:c2d52562f36b | 142 | confirmReceived = false; |
cotigac | 0:c2d52562f36b | 143 | |
cotigac | 0:c2d52562f36b | 144 | do |
cotigac | 0:c2d52562f36b | 145 | { |
cotigac | 0:c2d52562f36b | 146 | char * txBuff = (char *)txPacket; |
cotigac | 0:c2d52562f36b | 147 | uint8_t length = txPacket->length + gHostInterface_headerSize + 1; |
cotigac | 0:c2d52562f36b | 148 | |
cotigac | 0:c2d52562f36b | 149 | if(confirmRequested == true) |
cotigac | 0:c2d52562f36b | 150 | { |
cotigac | 0:c2d52562f36b | 151 | txPacket->start2 |= 0x01; |
cotigac | 0:c2d52562f36b | 152 | } |
cotigac | 0:c2d52562f36b | 153 | |
cotigac | 0:c2d52562f36b | 154 | for(uint8_t i = 0; i < length; i++) |
cotigac | 0:c2d52562f36b | 155 | { |
cotigac | 0:c2d52562f36b | 156 | device.putc(*txBuff); |
cotigac | 0:c2d52562f36b | 157 | txBuff++; |
cotigac | 0:c2d52562f36b | 158 | } |
cotigac | 0:c2d52562f36b | 159 | |
cotigac | 0:c2d52562f36b | 160 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 161 | DebugPrintTxPacket(txPacket); |
cotigac | 0:c2d52562f36b | 162 | #endif |
cotigac | 0:c2d52562f36b | 163 | |
cotigac | 0:c2d52562f36b | 164 | retries++; |
cotigac | 0:c2d52562f36b | 165 | |
cotigac | 0:c2d52562f36b | 166 | #if defined (gHostInterface_RxConfirmationEnable) |
cotigac | 0:c2d52562f36b | 167 | if((confirmRequested == true) && (confirmReceived == false)) |
cotigac | 0:c2d52562f36b | 168 | { |
cotigac | 0:c2d52562f36b | 169 | Thread::wait(gHostInterface_retransmitTimeout); |
cotigac | 0:c2d52562f36b | 170 | } |
cotigac | 0:c2d52562f36b | 171 | #endif |
cotigac | 0:c2d52562f36b | 172 | } |
cotigac | 0:c2d52562f36b | 173 | while((confirmRequested == true) && |
cotigac | 0:c2d52562f36b | 174 | (confirmReceived == false) && |
cotigac | 0:c2d52562f36b | 175 | (retries < gHostInterface_retransmitCount)); |
cotigac | 0:c2d52562f36b | 176 | } |
cotigac | 0:c2d52562f36b | 177 | |
cotigac | 0:c2d52562f36b | 178 | void KW40Z::mainTask(void) |
cotigac | 0:c2d52562f36b | 179 | { |
cotigac | 0:c2d52562f36b | 180 | mainThread.signal_wait(START_THREAD); |
cotigac | 0:c2d52562f36b | 181 | |
cotigac | 0:c2d52562f36b | 182 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 183 | pc.printf("MainTask Stared\r\n"); |
cotigac | 0:c2d52562f36b | 184 | #endif |
cotigac | 0:c2d52562f36b | 185 | |
cotigac | 0:c2d52562f36b | 186 | while(1) |
cotigac | 0:c2d52562f36b | 187 | { |
cotigac | 0:c2d52562f36b | 188 | osEvent evt = queue.get(); |
cotigac | 0:c2d52562f36b | 189 | if (evt.status == osEventMessage) |
cotigac | 0:c2d52562f36b | 190 | { |
cotigac | 0:c2d52562f36b | 191 | hostInterface_packet_t *rxPacket = (hostInterface_packet_t*)evt.value.p; |
cotigac | 0:c2d52562f36b | 192 | ProcessReceivedPacket(rxPacket); |
cotigac | 0:c2d52562f36b | 193 | mpool.free(rxPacket); |
cotigac | 0:c2d52562f36b | 194 | } |
cotigac | 0:c2d52562f36b | 195 | } |
cotigac | 0:c2d52562f36b | 196 | } |
cotigac | 0:c2d52562f36b | 197 | |
cotigac | 0:c2d52562f36b | 198 | void KW40Z::rxTask(void) |
cotigac | 0:c2d52562f36b | 199 | { |
cotigac | 0:c2d52562f36b | 200 | rxThread.signal_wait(START_THREAD); |
cotigac | 0:c2d52562f36b | 201 | |
cotigac | 0:c2d52562f36b | 202 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 203 | pc.printf("RxTask Stared\r\n"); |
cotigac | 0:c2d52562f36b | 204 | #endif |
cotigac | 0:c2d52562f36b | 205 | |
cotigac | 0:c2d52562f36b | 206 | while(1) |
cotigac | 0:c2d52562f36b | 207 | { |
cotigac | 0:c2d52562f36b | 208 | if(device.readable()) |
cotigac | 0:c2d52562f36b | 209 | { |
cotigac | 0:c2d52562f36b | 210 | *rxBuff++ = device.getc(); |
cotigac | 0:c2d52562f36b | 211 | ProcessBuffer(); |
cotigac | 0:c2d52562f36b | 212 | |
cotigac | 0:c2d52562f36b | 213 | /* check for buffer overflow */ |
cotigac | 0:c2d52562f36b | 214 | if(rxBuff >= ((uint8_t*)&hostInterface_rxPacket + sizeof(hostInterface_rxPacket))) |
cotigac | 0:c2d52562f36b | 215 | { |
cotigac | 0:c2d52562f36b | 216 | rxBuff = (uint8_t*)&hostInterface_rxPacket; |
cotigac | 0:c2d52562f36b | 217 | } |
cotigac | 0:c2d52562f36b | 218 | } |
cotigac | 0:c2d52562f36b | 219 | } |
cotigac | 0:c2d52562f36b | 220 | } |
cotigac | 0:c2d52562f36b | 221 | |
cotigac | 0:c2d52562f36b | 222 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 223 | void KW40Z::DebugPrintTxPacket(hostInterface_packet_t * txPacket) |
cotigac | 0:c2d52562f36b | 224 | { |
cotigac | 0:c2d52562f36b | 225 | char * txBuff = (char *)txPacket; |
cotigac | 0:c2d52562f36b | 226 | uint8_t length = txPacket->length + gHostInterface_headerSize + 1; |
cotigac | 0:c2d52562f36b | 227 | |
cotigac | 0:c2d52562f36b | 228 | pc.printf("Tx: "); |
cotigac | 0:c2d52562f36b | 229 | for(uint8_t i = 0; i < length; i++) |
cotigac | 0:c2d52562f36b | 230 | { |
cotigac | 0:c2d52562f36b | 231 | pc.printf("%02X ",*txBuff); |
cotigac | 0:c2d52562f36b | 232 | txBuff++; |
cotigac | 0:c2d52562f36b | 233 | } |
cotigac | 0:c2d52562f36b | 234 | pc.printf("\r\n"); |
cotigac | 0:c2d52562f36b | 235 | } |
cotigac | 0:c2d52562f36b | 236 | |
cotigac | 0:c2d52562f36b | 237 | void KW40Z::DebugPrintRxPacket() |
cotigac | 0:c2d52562f36b | 238 | { |
cotigac | 0:c2d52562f36b | 239 | pc.printf("RX: "); |
cotigac | 0:c2d52562f36b | 240 | for(uint8_t * i = (uint8_t*)&hostInterface_rxPacket; i<rxBuff; i++) |
cotigac | 0:c2d52562f36b | 241 | { |
cotigac | 0:c2d52562f36b | 242 | pc.printf("%02X ",*i); |
cotigac | 0:c2d52562f36b | 243 | } |
cotigac | 0:c2d52562f36b | 244 | pc.printf("\r\n"); |
cotigac | 0:c2d52562f36b | 245 | } |
cotigac | 0:c2d52562f36b | 246 | #endif |
cotigac | 0:c2d52562f36b | 247 | |
cotigac | 0:c2d52562f36b | 248 | void KW40Z::ProcessBuffer() |
cotigac | 0:c2d52562f36b | 249 | { |
cotigac | 0:c2d52562f36b | 250 | /* check if header has been received */ |
cotigac | 0:c2d52562f36b | 251 | if(rxBuff > ((uint8_t*)&hostInterface_rxPacket + gHostInterface_headerSize)) |
cotigac | 0:c2d52562f36b | 252 | { |
cotigac | 0:c2d52562f36b | 253 | /* check packet header */ |
cotigac | 0:c2d52562f36b | 254 | if((gHostInterface_startByte1 != hostInterface_rxPacket.start1)|| |
cotigac | 0:c2d52562f36b | 255 | (gHostInterface_startByte2 != (hostInterface_rxPacket.start2 & 0xFE))|| |
cotigac | 0:c2d52562f36b | 256 | (hostInterface_rxPacket.length > gHostInterface_dataSize)) |
cotigac | 0:c2d52562f36b | 257 | { |
cotigac | 0:c2d52562f36b | 258 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 259 | DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 260 | pc.printf("check header failed\r\n"); |
cotigac | 0:c2d52562f36b | 261 | #endif |
cotigac | 0:c2d52562f36b | 262 | |
cotigac | 0:c2d52562f36b | 263 | SearchStartByte(); |
cotigac | 0:c2d52562f36b | 264 | } |
cotigac | 0:c2d52562f36b | 265 | else |
cotigac | 0:c2d52562f36b | 266 | { |
cotigac | 0:c2d52562f36b | 267 | /* check data length */ |
cotigac | 0:c2d52562f36b | 268 | if(rxBuff > ((uint8_t*)&hostInterface_rxPacket + gHostInterface_headerSize + hostInterface_rxPacket.length)) |
cotigac | 0:c2d52562f36b | 269 | { |
cotigac | 0:c2d52562f36b | 270 | /* check trailer byte */ |
cotigac | 0:c2d52562f36b | 271 | if(gHostInterface_trailerByte != hostInterface_rxPacket.data[hostInterface_rxPacket.length]) |
cotigac | 0:c2d52562f36b | 272 | { |
cotigac | 0:c2d52562f36b | 273 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 274 | DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 275 | pc.printf("trailer byte failed\r\n"); |
cotigac | 0:c2d52562f36b | 276 | #endif |
cotigac | 0:c2d52562f36b | 277 | |
cotigac | 0:c2d52562f36b | 278 | SearchStartByte(); |
cotigac | 0:c2d52562f36b | 279 | } |
cotigac | 0:c2d52562f36b | 280 | else |
cotigac | 0:c2d52562f36b | 281 | { |
cotigac | 0:c2d52562f36b | 282 | |
cotigac | 0:c2d52562f36b | 283 | #if defined (gHostInterface_RxConfirmationEnable) |
cotigac | 0:c2d52562f36b | 284 | if(hostInterface_rxPacket.type == packetType_OK) |
cotigac | 0:c2d52562f36b | 285 | { |
cotigac | 0:c2d52562f36b | 286 | confirmReceived = true; |
cotigac | 0:c2d52562f36b | 287 | } |
cotigac | 0:c2d52562f36b | 288 | #endif |
cotigac | 0:c2d52562f36b | 289 | |
cotigac | 0:c2d52562f36b | 290 | /* send message to main task */ |
cotigac | 0:c2d52562f36b | 291 | hostInterface_packet_t *rxPacket = mpool.alloc(); |
cotigac | 0:c2d52562f36b | 292 | memcpy(rxPacket, &hostInterface_rxPacket, sizeof(hostInterface_packet_t)); |
cotigac | 0:c2d52562f36b | 293 | queue.put(rxPacket); |
cotigac | 0:c2d52562f36b | 294 | |
cotigac | 0:c2d52562f36b | 295 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 296 | DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 297 | #endif |
cotigac | 0:c2d52562f36b | 298 | /* reset buffer position */ |
cotigac | 0:c2d52562f36b | 299 | rxBuff = (uint8_t*)&hostInterface_rxPacket; |
cotigac | 0:c2d52562f36b | 300 | } |
cotigac | 0:c2d52562f36b | 301 | } |
cotigac | 0:c2d52562f36b | 302 | } |
cotigac | 0:c2d52562f36b | 303 | } |
cotigac | 0:c2d52562f36b | 304 | } |
cotigac | 0:c2d52562f36b | 305 | |
cotigac | 0:c2d52562f36b | 306 | void KW40Z::SearchStartByte() |
cotigac | 0:c2d52562f36b | 307 | { |
cotigac | 0:c2d52562f36b | 308 | bool found = false; |
cotigac | 0:c2d52562f36b | 309 | uint8_t * rdIdx = (uint8_t*)&hostInterface_rxPacket + 1; |
cotigac | 0:c2d52562f36b | 310 | |
cotigac | 0:c2d52562f36b | 311 | while(rdIdx < rxBuff) |
cotigac | 0:c2d52562f36b | 312 | { |
cotigac | 0:c2d52562f36b | 313 | if(*rdIdx == gHostInterface_startByte1) |
cotigac | 0:c2d52562f36b | 314 | { |
cotigac | 0:c2d52562f36b | 315 | uint32_t len = rxBuff - rdIdx; |
cotigac | 0:c2d52562f36b | 316 | |
cotigac | 0:c2d52562f36b | 317 | memcpy(&hostInterface_rxPacket,rdIdx,len); |
cotigac | 0:c2d52562f36b | 318 | rxBuff -= len; |
cotigac | 0:c2d52562f36b | 319 | found = true; |
cotigac | 0:c2d52562f36b | 320 | |
cotigac | 0:c2d52562f36b | 321 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 322 | pc.printf("moving "); |
cotigac | 0:c2d52562f36b | 323 | #endif |
cotigac | 0:c2d52562f36b | 324 | break; |
cotigac | 0:c2d52562f36b | 325 | } |
cotigac | 0:c2d52562f36b | 326 | rdIdx++; |
cotigac | 0:c2d52562f36b | 327 | } |
cotigac | 0:c2d52562f36b | 328 | |
cotigac | 0:c2d52562f36b | 329 | if(!found) |
cotigac | 0:c2d52562f36b | 330 | { |
cotigac | 0:c2d52562f36b | 331 | /* reset buffer position */ |
cotigac | 0:c2d52562f36b | 332 | rxBuff = (uint8_t*)&hostInterface_rxPacket; |
cotigac | 0:c2d52562f36b | 333 | } |
cotigac | 0:c2d52562f36b | 334 | |
cotigac | 0:c2d52562f36b | 335 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 336 | pc.printf("search done\r\n"); |
cotigac | 0:c2d52562f36b | 337 | DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 338 | #endif |
cotigac | 0:c2d52562f36b | 339 | } |
cotigac | 0:c2d52562f36b | 340 | |
cotigac | 0:c2d52562f36b | 341 | void KW40Z::SendPacketOK() |
cotigac | 0:c2d52562f36b | 342 | { |
cotigac | 0:c2d52562f36b | 343 | hostInterface_packet_t txPacket = {0}; |
cotigac | 0:c2d52562f36b | 344 | |
cotigac | 0:c2d52562f36b | 345 | txPacket.start1 = gHostInterface_startByte1, |
cotigac | 0:c2d52562f36b | 346 | txPacket.start2 = gHostInterface_startByte2, |
cotigac | 0:c2d52562f36b | 347 | txPacket.type = packetType_OK, |
cotigac | 0:c2d52562f36b | 348 | txPacket.length = 0, |
cotigac | 0:c2d52562f36b | 349 | txPacket.data[0] = gHostInterface_trailerByte; |
cotigac | 0:c2d52562f36b | 350 | |
cotigac | 0:c2d52562f36b | 351 | SendPacket(&txPacket, false); |
cotigac | 0:c2d52562f36b | 352 | } |
cotigac | 0:c2d52562f36b | 353 | |
cotigac | 0:c2d52562f36b | 354 | void KW40Z::ConfirmPacketOK() |
cotigac | 0:c2d52562f36b | 355 | { |
cotigac | 0:c2d52562f36b | 356 | } |
cotigac | 0:c2d52562f36b | 357 | |
cotigac | 0:c2d52562f36b | 358 | void KW40Z::ProcessReceivedPacket(hostInterface_packet_t * rxPacket) |
cotigac | 0:c2d52562f36b | 359 | { |
cotigac | 0:c2d52562f36b | 360 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 361 | pc.printf("packet found %d\r\n", rxPacket->type); |
cotigac | 0:c2d52562f36b | 362 | #endif |
cotigac | 0:c2d52562f36b | 363 | |
cotigac | 0:c2d52562f36b | 364 | #ifdef gHostInterface_TxConfirmationEnable |
cotigac | 0:c2d52562f36b | 365 | // acknowledge the packet reception |
cotigac | 0:c2d52562f36b | 366 | if ( 1 == ( rxPacket->start2 & 0x01 ) ) |
cotigac | 0:c2d52562f36b | 367 | { |
cotigac | 0:c2d52562f36b | 368 | SendPacketOK(); |
cotigac | 0:c2d52562f36b | 369 | } |
cotigac | 0:c2d52562f36b | 370 | #endif |
cotigac | 0:c2d52562f36b | 371 | |
cotigac | 0:c2d52562f36b | 372 | switch(rxPacket->type) |
cotigac | 0:c2d52562f36b | 373 | { |
cotigac | 0:c2d52562f36b | 374 | /* button presses */ |
cotigac | 0:c2d52562f36b | 375 | case packetType_pressUp: |
cotigac | 1:f6f9b24aea57 | 376 | if(buttonUpCb != NULL) buttonUpCb(); |
cotigac | 1:f6f9b24aea57 | 377 | break; |
cotigac | 1:f6f9b24aea57 | 378 | |
cotigac | 0:c2d52562f36b | 379 | case packetType_pressDown: |
cotigac | 1:f6f9b24aea57 | 380 | if(buttonDownCb != NULL) buttonDownCb(); |
cotigac | 1:f6f9b24aea57 | 381 | break; |
cotigac | 1:f6f9b24aea57 | 382 | |
cotigac | 0:c2d52562f36b | 383 | case packetType_pressLeft: |
cotigac | 1:f6f9b24aea57 | 384 | if(buttonLeftCb != NULL) buttonLeftCb(); |
cotigac | 1:f6f9b24aea57 | 385 | break; |
cotigac | 1:f6f9b24aea57 | 386 | |
cotigac | 0:c2d52562f36b | 387 | case packetType_pressRight: |
cotigac | 1:f6f9b24aea57 | 388 | if(buttonRightCb != NULL) buttonRightCb(); |
cotigac | 1:f6f9b24aea57 | 389 | break; |
cotigac | 1:f6f9b24aea57 | 390 | |
cotigac | 0:c2d52562f36b | 391 | case packetType_slide: |
cotigac | 1:f6f9b24aea57 | 392 | if(buttonSlideCb != NULL) buttonSlideCb(); |
cotigac | 0:c2d52562f36b | 393 | break; |
cotigac | 0:c2d52562f36b | 394 | |
cotigac | 0:c2d52562f36b | 395 | /* Alert Service */ |
cotigac | 0:c2d52562f36b | 396 | case packetType_alertIn: |
cotigac | 1:f6f9b24aea57 | 397 | if(alertCb != NULL) alertCb(&rxPacket->data[0], rxPacket->length); |
cotigac | 0:c2d52562f36b | 398 | break; |
cotigac | 0:c2d52562f36b | 399 | |
cotigac | 0:c2d52562f36b | 400 | /* Passkey for pairing received */ |
cotigac | 0:c2d52562f36b | 401 | case packetType_passDisplay: |
cotigac | 1:f6f9b24aea57 | 402 | if(passkeyCb != NULL) passkeyCb(&rxPacket->data[0]); |
cotigac | 0:c2d52562f36b | 403 | break; |
cotigac | 0:c2d52562f36b | 404 | |
cotigac | 0:c2d52562f36b | 405 | /* OTAP messages */ |
cotigac | 0:c2d52562f36b | 406 | case packetType_otapCompleted: |
cotigac | 0:c2d52562f36b | 407 | case packetType_otapFailed: |
cotigac | 0:c2d52562f36b | 408 | break; |
cotigac | 0:c2d52562f36b | 409 | |
cotigac | 0:c2d52562f36b | 410 | /* TSI Status */ |
cotigac | 0:c2d52562f36b | 411 | case packetType_buttonsGroupSendActive: |
cotigac | 0:c2d52562f36b | 412 | break; |
cotigac | 0:c2d52562f36b | 413 | |
cotigac | 0:c2d52562f36b | 414 | /* Advertisement Mode Info */ |
cotigac | 0:c2d52562f36b | 415 | case packetType_advModeSend: |
cotigac | 0:c2d52562f36b | 416 | break; |
cotigac | 0:c2d52562f36b | 417 | |
cotigac | 0:c2d52562f36b | 418 | /* Link State */ |
cotigac | 0:c2d52562f36b | 419 | case packetType_linkStateSend: |
cotigac | 0:c2d52562f36b | 420 | break; |
cotigac | 0:c2d52562f36b | 421 | |
cotigac | 0:c2d52562f36b | 422 | /* ANCS Service Notification Received */ |
cotigac | 0:c2d52562f36b | 423 | case packetType_notification: |
cotigac | 1:f6f9b24aea57 | 424 | if(notificationsCb != NULL) notificationsCb(rxPacket->data[0], rxPacket->data[1]); |
cotigac | 0:c2d52562f36b | 425 | break; |
cotigac | 0:c2d52562f36b | 426 | |
cotigac | 0:c2d52562f36b | 427 | /* Build version */ |
cotigac | 0:c2d52562f36b | 428 | case packetType_buildVersion: |
cotigac | 0:c2d52562f36b | 429 | break; |
cotigac | 0:c2d52562f36b | 430 | |
cotigac | 0:c2d52562f36b | 431 | case packetType_OK: |
cotigac | 0:c2d52562f36b | 432 | /* do nothing, the flag is set in the RxTask */ |
cotigac | 0:c2d52562f36b | 433 | break; |
cotigac | 0:c2d52562f36b | 434 | |
cotigac | 0:c2d52562f36b | 435 | default: |
cotigac | 0:c2d52562f36b | 436 | break; |
cotigac | 0:c2d52562f36b | 437 | } |
cotigac | 0:c2d52562f36b | 438 | } |