00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __HRWB_H__
00013 #define __HRWB_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #pragma systemFile
00037
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041
00042
00043 #define HRWB_I2C_ADDR 0x20
00044 #define HRWB_SIMPLE_WRITE 0x01
00045 #define HRWB_MASKED_WRITE 0x02
00046
00047 #define HRWB_SENSORTYPE 0x0008
00048 #define HRWB_BATTLVL 0x000C
00049
00050 #define HRWB_HTTP_RESULT_CODE 0x001F
00051
00052 #define HRWB_RESET 0x000E
00053 #define HRWB_RESET_WIFIBLOCK 0x00
00054 #define HRWB_RESET_ERRORCODE 0x01
00055
00056
00057 #define HRWB_WIFI_STATUS 0x0200
00058 #define HRWB_WIFI_SIGNAL 0x0201
00059 #define HRWB_WIFI_SSID 0x0202
00060 #define HRWB_WIFI_IP_ADDR 0x0222
00061 #define HRWB_WIFI_IP_MASK 0x0226
00062 #define HRWB_WIFI_IP_GATEWAY 0x022A
00063 #define HRWB_WIFI_SECURITY_TYPE 0x022E
00064 #define HRWB_WIFI_SECURITY_KEY 0x022F
00065 #define HRWB_WIFI_SCANRESULT 0x026F
00066 #define HRWB_WIFI_SCANSEL 0x0270
00067 #define HRWB_WIFI_SCANSIGNAL 0x0271
00068 #define HRWB_WIFI_SCANSEC 0x0272
00069 #define HRWB_WIFI_SCANSSID 0x0273
00070
00071 #define HRWB_WIFI_STATUS_ENABLE 0x01
00072 #define HRWB_WIFI_STATUS_SAVE_CONF 0x02
00073 #define HRWB_WIFI_STATUS_START_SCAN 0x04
00074 #define HRWB_WIFI_STATUS_SCAN_DONE 0x08
00075 #define HRWB_WIFI_STATUS_CONN_STAT 0x10
00076 #define HRWB_WIFI_STATUS_RESERVED 0x20
00077 #define HRWB_WIFI_STATUS_RECONN 0x40
00078 #define HRWB_WIFI_STATUS_DEL_CONF 0x80
00079
00080 #define HRWB_SECURITY_OPEN 0x00
00081 #define HRWB_SECURITY_WEP 0x01
00082 #define HRWB_SECURITY_WPA 0x02
00083 #define HRWB_SECURITY_WPA2 0x03
00084
00085
00086 #define HRWB_GET_STATUS 0x1000
00087 #define HRWB_GET_LENGTH 0x1001
00088 #define HRWB_GET_IP_ADDR 0x1002
00089 #define HRWB_GET_PORT 0x1006
00090 #define HRWB_GET_URL 0x1008
00091 #define HRWB_GET_DATA 0x1048
00092
00093 #define HRWB_GET_STATUS_READY 0x01
00094 #define HRWB_GET_STATUS_RESERVED1 0x02
00095 #define HRWB_GET_STATUS_SEND_REQ 0x04
00096 #define HRWB_GET_STATUS_REQ_DONE 0x08
00097 #define HRWB_GET_STATUS_RESERVED2 0x10
00098 #define HRWB_GET_STATUS_RESERVED3 0x20
00099 #define HRWB_GET_STATUS_RESERVED4 0x40
00100 #define HRWB_GET_STATUS_REQ_ERROR 0x80
00101
00102
00103 #define HRWB_POST_STATUS 0x2000
00104 #define HRWB_POST_LENGTH 0x2001
00105 #define HRWB_POST_IP_ADDR 0x2002
00106 #define HRWB_POST_PORT 0x2006
00107 #define HRWB_POST_URL 0x2008
00108 #define HRWB_POST_DATA 0x2048
00109
00110 #define HRWB_POST_STATUS_READY 0x01
00111 #define HRWB_POST_STATUS_RESERVED1 0x02
00112 #define HRWB_POST_STATUS_SEND_REQ 0x04
00113 #define HRWB_POST_STATUS_REQ_DONE 0x08
00114 #define HRWB_POST_STATUS_EN_FL_STO 0x10
00115 #define HRWB_POST_STATUS_EN_FL_RTX 0x20
00116
00117 #define HRWB_POST_STATUS_RESERVED2 0x40
00118 #define HRWB_POST_STATUS_REQ_ERROR 0x80
00119
00120
00121 #define HRWB_TCP_STATUS 0x4000
00122 #define HRWB_TCP_IP_ADDR 0x4001
00123 #define HRWB_TCP_PORT 0x4005
00124 #define HRWB_TCP_TX_LENGTH 0x4007
00125 #define HRWB_TCP_TX_DATA 0x4008
00126 #define HRWB_TCP_RX_LENGTH 0x4028
00127 #define HRWB_TCP_RX_DATA 0x4029
00128
00129 #define HRWB_TCP_STATUS_READY 0x01
00130 #define HRWB_TCP_STATUS_SEND_REQ 0x02
00131 #define HRWB_TCP_STATUS_REQ_DONE 0x04
00132 #define HRWB_TCP_STATUS_RESERVED1 0x08
00133 #define HRWB_TCP_STATUS_RESERVED2 0x10
00134 #define HRWB_TCP_STATUS_RESERVED3 0x20
00135 #define HRWB_TCP_STATUS_RESERVED4 0x40
00136 #define HRWB_TCP_STATUS_REQ_ERROR 0x80
00137
00138
00139 #define HRWB_UDP_STATUS 0x3000
00140 #define HRWB_UDP_IP_ADDR 0x3001
00141 #define HRWB_UDP_DPORT 0x3005
00142 #define HRWB_UDP_SPORT 0x3007
00143 #define HRWB_UDP_TX_LENGTH 0x3009
00144 #define HRWB_UDP_TX_DATA 0x300A
00145 #define HRWB_UDP_RX_LENGTH 0x302A
00146 #define HRWB_UDP_RX_DATA 0x302B
00147
00148 #define HRWB_UDP_STATUS_READY 0x01
00149 #define HRWB_UDP_STATUS_SEND_REQ 0x02
00150 #define HRWB_UDP_STATUS_REQ_DONE 0x04
00151 #define HRWB_UDP_STATUS_RXD_RDY 0x08
00152 #define HRWB_UDP_STATUS_RXD_READ 0x10
00153 #define HRWB_UDP_STATUS_RESERVED1 0x20
00154 #define HRWB_UDP_STATUS_RESERVED2 0x40
00155 #define HRWB_UDP_STATUS_REQ_ERROR 0x80
00156
00157 tByteArray HRWB_I2CRequest;
00158 tByteArray HRWB_I2CReply;
00159 tByteArray HRWB_scratch;
00160 tHugeByteArray HRWB_HugeArray;
00161
00162
00163 typedef struct
00164 {
00165 string wifi_ssid;
00166 string wifi_pass;
00167 tIPaddr IP;
00168 tIPaddr netmask;
00169 tIPaddr gateway;
00170 ubyte wifi_type;
00171 } tNetworkInfo;
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 typedef struct
00184 {
00185 tIPaddr IP;
00186 int port;
00187 tHugeByteArray URL;
00188 tBigByteArray RXData;
00189 ubyte RXDataLen;
00190 ubyte result_code;
00191 } tGetRequest;
00192
00193
00194 typedef struct
00195 {
00196 tIPaddr IP;
00197 int port;
00198 tHugeByteArray URL;
00199 tBigByteArray TXData;
00200 ubyte TXDataLen;
00201 ubyte result_code;
00202 } tPostRequest;
00203
00204
00205 typedef struct
00206 {
00207 tIPaddr IP;
00208 int port;
00209 tHugeByteArray TXData;
00210 ubyte TXDataLen;
00211 tHugeByteArray RXData;
00212 ubyte RXDataLen;
00213 ubyte result_code;
00214 } tTCPRequest;
00215
00216
00217 typedef struct
00218 {
00219 tIPaddr IP;
00220 int sport;
00221 int dport;
00222 tHugeByteArray TXData;
00223 ubyte TXDataLen;
00224 tHugeByteArray RXData;
00225 ubyte RXDataLen;
00226 ubyte result_code;
00227 } tUDPRequest;
00228
00229
00230
00231
00232
00233
00234
00235
00236 int strsize(tHugeByteArray &array)
00237 {
00238 for (int i = 0; i < sizeof(tHugeByteArray); i++)
00239 {
00240 if((ubyte)array[i] == 0)
00241 return i;
00242 }
00243 return sizeof(tHugeByteArray);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253 void stringToIp(const string address, tIPaddr &addr)
00254 {
00255 string octet;
00256 string copy;
00257 int index = 0;
00258 memcpy(copy, address, 20);
00259
00260 for (int i = 0; i < 3; i++)
00261 {
00262 octet = "";
00263 index = StringFind(copy, ".");
00264 memcpy(octet, copy, index);
00265 addr[i] = atoi(octet);
00266 StringDelete(copy, 0, index+1);
00267 }
00268 addr[3] = atoi(copy);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 bool HRWBwriteRegMasked(tSensors link, int reg, ubyte data, ubyte mask)
00281 {
00282
00283 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00284
00285 HRWB_I2CRequest[0] = 6;
00286 HRWB_I2CRequest[1] = HRWB_I2C_ADDR;
00287 HRWB_I2CRequest[2] = HRWB_MASKED_WRITE;
00288 HRWB_I2CRequest[3] = (reg >> 8) & 0xFF;
00289 HRWB_I2CRequest[4] = reg & 0xFF;
00290 HRWB_I2CRequest[5] = mask;
00291 HRWB_I2CRequest[6] = data;
00292
00293 return writeI2C(link, HRWB_I2CRequest);
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 bool HRWBwriteReg(tSensors link, int reg, ubyte data)
00305 {
00306
00307 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00308
00309 HRWB_I2CRequest[0] = 5;
00310 HRWB_I2CRequest[1] = HRWB_I2C_ADDR;
00311 HRWB_I2CRequest[2] = HRWB_SIMPLE_WRITE;
00312 HRWB_I2CRequest[3] = (reg >> 8) & 0xFF;
00313 HRWB_I2CRequest[4] = reg & 0xFF;
00314 HRWB_I2CRequest[5] = data;
00315
00316 return writeI2C(link, HRWB_I2CRequest);
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 bool _HRWBwriteRegPrivate(tSensors link, int reg, tByteArray &data, int size)
00331 {
00332 if (size > 12)
00333 return false;
00334
00335 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00336
00337 HRWB_I2CRequest[0] = 4 + size;
00338 HRWB_I2CRequest[1] = HRWB_I2C_ADDR;
00339 HRWB_I2CRequest[2] = HRWB_SIMPLE_WRITE;
00340 HRWB_I2CRequest[3] = (reg >> 8) & 0xFF;
00341 HRWB_I2CRequest[4] = reg & 0xFF;
00342
00343 memcpy(&HRWB_I2CRequest[5], data, size);
00344
00345 return writeI2C(link, HRWB_I2CRequest);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 bool HRWBwriteReg(tSensors link, int reg, tByteArray &data, int size)
00358 {
00359 tByteArray tmpBuff;
00360
00361 int bytesleft = size;
00362 int datalen = 0;
00363
00364 for (int i = 0; i < ((size/12) + 1); i++)
00365 {
00366 datalen = (bytesleft > 12) ? 12 : bytesleft;
00367 memset(tmpBuff, 0, sizeof(tmpBuff));
00368 memcpy(tmpBuff, &data[i*12], datalen);
00369
00370 if (!_HRWBwriteRegPrivate(link, reg + (i*12), tmpBuff, datalen))
00371 return false;
00372 bytesleft -= 12;
00373 }
00374 return true;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 bool HRWBwriteReg(tSensors link, int reg, tHugeByteArray &data, int size)
00387 {
00388 tByteArray tmpBuff;
00389 int bytesleft = size;
00390 int datalen = 0;
00391
00392 for (int i = 0; i < ((size/12) + 1); i++)
00393 {
00394 datalen = (bytesleft > 12) ? 12 : bytesleft;
00395 memset(tmpBuff, 0, sizeof(tmpBuff));
00396 memcpy(tmpBuff, &data[i*12], datalen);
00397
00398 if (!_HRWBwriteRegPrivate(link, reg + (i*12), tmpBuff, datalen))
00399 return false;
00400 bytesleft -= 12;
00401 }
00402 return true;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 bool HRWBwriteReg(tSensors link, int reg, tBigByteArray &data, int size)
00415 {
00416 tByteArray tmpBuff;
00417 int bytesleft = size;
00418 int datalen = 0;
00419
00420 for (int i = 0; i < ((size/12) + 1); i++)
00421 {
00422 datalen = (bytesleft > 12) ? 12 : bytesleft;
00423 memset(tmpBuff, 0, sizeof(tmpBuff));
00424 memcpy(tmpBuff, &data[i*12], datalen);
00425
00426 if (!_HRWBwriteRegPrivate(link, reg + (i*12), tmpBuff, datalen))
00427 return false;
00428 bytesleft -= 12;
00429 }
00430 return true;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 bool HRWBreadReg(tSensors link, int reg, int size)
00442 {
00443
00444 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00445
00446 HRWB_I2CRequest[0] = 7;
00447 HRWB_I2CRequest[1] = HRWB_I2C_ADDR;
00448 HRWB_I2CRequest[2] = HRWB_SIMPLE_WRITE;
00449 HRWB_I2CRequest[3] = 0x01;
00450 HRWB_I2CRequest[4] = 0x00;
00451 HRWB_I2CRequest[5] = (reg >> 8) & 0xFF;
00452 HRWB_I2CRequest[6] = reg & 0xFF;
00453 HRWB_I2CRequest[7] = size & 0xFF;
00454
00455 return writeI2C(link, HRWB_I2CRequest, HRWB_I2CReply, size);
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 bool HRWBreadBigReg(tSensors link, int reg, int size)
00467 {
00468
00469 int bytesleft = size;
00470 int requestlen = 0;
00471 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00472 memset(HRWB_HugeArray, 0, sizeof(HRWB_HugeArray));
00473
00474 for (int i = 0; i < ((size/16) + 1); i++)
00475 {
00476 requestlen = (bytesleft > 16) ? 16 : bytesleft;
00477 if (!HRWBreadReg(link, reg + (i*16), requestlen))
00478 return false;
00479 memcpy(&HRWB_HugeArray[16 * i], HRWB_I2CReply, requestlen);
00480 bytesleft -= 16;
00481 wait1Msec(10);
00482 }
00483 return true;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492 int HRWBreadHTTPCode(tSensors link)
00493 {
00494 string tmpResultCode;
00495 HRWBreadReg(WifiBlockPort, HRWB_HTTP_RESULT_CODE, 3);
00496 HRWB_I2CReply[3] = 0;
00497 memcpy(tmpResultCode, HRWB_I2CReply, 4);
00498 return atoi(tmpResultCode);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 bool HRWBresetWifiBlock(tSensors link)
00508 {
00509
00510 return HRWBwriteRegMasked(WifiBlockPort, HRWB_RESET, 1, HRWB_RESET_WIFIBLOCK);
00511 }
00512
00513
00514
00515
00516
00517
00518
00519 float HRWBreadBatt(tSensors link)
00520 {
00521 HRWBreadReg(link, HRWB_BATTLVL, 1);
00522 return (float)HRWB_I2CReply[0]/10.0;
00523
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 bool HRWBreadSensorType (tSensors link, string &sType)
00535 {
00536 if (!HRWBreadReg(link, HRWB_SENSORTYPE, 4))
00537 return false;
00538
00539 StringFromChars(sType, &HRWB_I2CReply);
00540
00541 return true;
00542 }
00543
00544
00545
00546
00547
00548
00549
00550 bool HRWBenableWifi(tSensors link)
00551 {
00552
00553 return HRWBwriteRegMasked(link, HRWB_WIFI_STATUS, 0x01, HRWB_WIFI_STATUS_ENABLE);
00554 }
00555
00556
00557
00558
00559
00560
00561
00562 bool HRWBdisableWifi(tSensors link)
00563 {
00564
00565 return HRWBwriteRegMasked(link, HRWB_WIFI_STATUS, 0x00, HRWB_WIFI_STATUS_ENABLE);
00566 }
00567
00568
00569
00570
00571
00572
00573
00574 bool HRWBsaveConfig(tSensors link)
00575 {
00576
00577 return HRWBwriteRegMasked(link, HRWB_WIFI_STATUS, 0x02, HRWB_WIFI_STATUS_SAVE_CONF);
00578 }
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 bool HRWBeraseConfig(tSensors link)
00589 {
00590
00591 return HRWBwriteRegMasked(link, HRWB_WIFI_STATUS, 0x80, HRWB_WIFI_STATUS_DEL_CONF);
00592 }
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 bool HRWBconfigNetwork(tSensors link, tNetworkInfo &netInfo) {
00682 bool done = false;
00683 ubyte status;
00684 memset(HRWB_I2CRequest, 0, sizeof(tByteArray));
00685
00686
00687 if (!HRWBdisableWifi(link))
00688 return false;
00689
00690 if (!HRWBeraseConfig(link))
00691 return false;
00692 wait1Msec(100);
00693
00694 PlaySound(soundBlip);
00695 memcpy(HRWB_scratch, netInfo.IP[0], sizeof(tIPaddr));
00696 if (!HRWBwriteReg(link, HRWB_WIFI_IP_ADDR, HRWB_scratch, sizeof(tIPaddr)))
00697 return false;
00698
00699 memcpy(HRWB_scratch, netInfo.netmask[0], sizeof(tIPaddr));
00700 if (!HRWBwriteReg(link, HRWB_WIFI_IP_MASK, HRWB_scratch, sizeof(tIPaddr)))
00701 return false;
00702
00703 memcpy(HRWB_scratch, netInfo.gateway[0], sizeof(tIPaddr));
00704 if (!HRWBwriteReg(link, HRWB_WIFI_IP_GATEWAY, HRWB_scratch, sizeof(tIPaddr)))
00705 return false;
00706
00707 if (!HRWBwriteReg(link, HRWB_WIFI_SECURITY_TYPE, netInfo.wifi_type))
00708 return false;
00709
00710 memcpy(HRWB_scratch, netInfo.wifi_ssid, strlen(netInfo.wifi_ssid) + 1);
00711 if (!HRWBwriteReg(link, HRWB_WIFI_SSID, HRWB_scratch, strlen(netInfo.wifi_ssid) + 1))
00712 return false;
00713
00714 memcpy(HRWB_scratch, netInfo.wifi_pass, strlen(netInfo.wifi_pass) + 1);
00715 if (!HRWBwriteReg(link, HRWB_WIFI_SECURITY_KEY, HRWB_scratch, strlen(netInfo.wifi_pass) + 1))
00716 return false;
00717
00718 if (!HRWBwriteRegMasked(link, HRWB_WIFI_STATUS, HRWB_WIFI_STATUS_RECONN, HRWB_WIFI_STATUS_RECONN))
00719 return false;
00720
00721 if (!HRWBsaveConfig(link))
00722 return false;
00723
00724 if (!HRWBenableWifi(link))
00725 return false;
00726
00727 while(!done) {
00728 if (!HRWBreadReg(link, HRWB_WIFI_STATUS, 1))
00729 return false;
00730 status = HRWB_I2CReply[0];
00731
00732 if ((status & HRWB_WIFI_STATUS_CONN_STAT) == HRWB_WIFI_STATUS_CONN_STAT)
00733 {
00734 done = true;
00735 }
00736 wait1Msec(100);
00737 }
00738
00739 return true;
00740 }
00741
00742
00743
00744
00745
00746
00747
00748
00749 bool HRWBdoGET(tSensors link, tGetRequest &getrequest) {
00750 ubyte status = 0;
00751 bool done = false;
00752 ubyte tmpPort[2];
00753
00754 tmpPort[0] = (getrequest.port >> 8) & 0xFF;
00755 tmpPort[1] = (getrequest.port >> 0) & 0xFF;
00756
00757 HRWBreadReg(link, HRWB_GET_STATUS, 1);
00758 status = HRWB_I2CReply[0];
00759
00760 if (status & HRWB_GET_STATUS_READY != HRWB_GET_STATUS_READY)
00761 {
00762 writeDebugStreamLine("NOT READY");
00763 return false;
00764 }
00765
00766 memcpy(HRWB_scratch, getrequest.IP[0], sizeof(tIPaddr));
00767 if (!HRWBwriteReg(link, HRWB_GET_IP_ADDR, HRWB_scratch, sizeof(tIPaddr)))
00768 return false;
00769
00770 memcpy(HRWB_scratch, tmpPort, 2);
00771 if (!HRWBwriteReg(link, HRWB_GET_PORT, HRWB_scratch, 2))
00772 return false;
00773
00774 if (!HRWBwriteReg(link, HRWB_GET_URL, (tHugeByteArray)getrequest.URL, strsize(getrequest.URL) + 1))
00775 return false;
00776
00777 if (!HRWBwriteRegMasked(link, HRWB_GET_STATUS, HRWB_GET_STATUS_SEND_REQ, HRWB_GET_STATUS_SEND_REQ))
00778 return false;
00779
00780 while(!done) {
00781 if (!HRWBreadReg(link, HRWB_GET_STATUS, 1))
00782 return false;
00783
00784 status = HRWB_I2CReply[0];
00785
00786 if ((status & HRWB_GET_STATUS_REQ_ERROR) == HRWB_GET_STATUS_REQ_ERROR)
00787 {
00788
00789 getrequest.result_code = HRWBreadHTTPCode(link);
00790
00791 done = true;
00792 }
00793 else if ((status & HRWB_GET_STATUS_REQ_DONE) == HRWB_GET_STATUS_REQ_DONE)
00794 {
00795
00796 getrequest.result_code = HRWBreadHTTPCode(link);
00797
00798 done = true;
00799 }
00800 wait1Msec(100);
00801 }
00802
00803 wait1Msec(1000);
00804 if (!HRWBreadReg(link, HRWB_GET_LENGTH, 1))
00805 return false;
00806 getrequest.RXDataLen = HRWB_I2CReply[0];
00807
00808 writeDebugStreamLine("res length: %d", getrequest.RXDataLen);
00809 if (!HRWBreadBigReg(link, HRWB_GET_DATA, getrequest.RXDataLen))
00810 return false;
00811
00812 memcpy(&getrequest.RXData[0], &HRWB_HugeArray[0], sizeof(tBigByteArray));
00813 return true;
00814
00815
00816 }
00817
00818
00819
00820
00821
00822
00823
00824
00825 bool HRWBdoPOST(tSensors link, tPostRequest &postrequest) {
00826 ubyte status = 0;
00827 bool done = false;
00828 ubyte tmpPort[2];
00829
00830 tmpPort[0] = (postrequest.port >> 8) & 0xFF;
00831 tmpPort[1] = (postrequest.port >> 0) & 0xFF;
00832
00833 HRWBreadReg(link, HRWB_POST_STATUS, 1);
00834 status = HRWB_I2CReply[0];
00835
00836 if (status & HRWB_POST_STATUS_READY != HRWB_POST_STATUS_READY)
00837 return false;
00838
00839
00840 memcpy(HRWB_scratch, postrequest.IP[0], sizeof(tIPaddr));
00841 if (!HRWBwriteReg(link, HRWB_POST_IP_ADDR, HRWB_scratch, sizeof(tIPaddr)))
00842 return false;
00843
00844 memcpy(HRWB_scratch, tmpPort, 2);
00845 if (!HRWBwriteReg(link, HRWB_POST_PORT, HRWB_scratch, 2))
00846 return false;
00847
00848 if (!HRWBwriteReg(link, HRWB_POST_URL, (tHugeByteArray)postrequest.URL, strsize(postrequest.URL) + 1))
00849 return false;
00850
00851 if (!HRWBwriteReg(link, HRWB_POST_DATA, (tBigByteArray)postrequest.TXData[0], postrequest.TXDataLen))
00852 return false;
00853
00854 if (!HRWBwriteRegMasked(link, HRWB_POST_STATUS, HRWB_POST_STATUS_SEND_REQ, HRWB_POST_STATUS_SEND_REQ))
00855 return false;
00856
00857 while(!done) {
00858 if (!HRWBreadReg(link, HRWB_POST_STATUS, 1))
00859 return false;
00860 status = HRWB_I2CReply[0];
00861
00862
00863 if ((status & HRWB_POST_STATUS_REQ_ERROR) == HRWB_POST_STATUS_REQ_ERROR)
00864 {
00865
00866 postrequest.result_code = HRWBreadHTTPCode(link);
00867
00868 done = true;
00869 }
00870 else if ((status & HRWB_POST_STATUS_REQ_DONE) == HRWB_POST_STATUS_REQ_DONE)
00871 {
00872
00873 postrequest.result_code = HRWBreadHTTPCode(link);
00874
00875 done = true;
00876 }
00877 wait1Msec(100);
00878 }
00879 return true;
00880 }
00881
00882
00883
00884
00885
00886
00887
00888
00889 bool HRWBdoTCP(tSensors link, tTCPRequest &tcprequest) {
00890 ubyte status = 0;
00891 bool done = false;
00892 HRWBreadReg(link, HRWB_TCP_STATUS, 1);
00893 status = HRWB_I2CReply[0];
00894 ubyte tmpPort[2];
00895
00896 tmpPort[0] = (tcprequest.port >> 8) & 0xFF;
00897 tmpPort[1] = (tcprequest.port >> 0) & 0xFF;
00898
00899 if (status & HRWB_TCP_STATUS_READY != HRWB_TCP_STATUS_READY)
00900 return false;
00901
00902
00903 memcpy(HRWB_scratch, tcprequest.IP[0], sizeof(tIPaddr));
00904 if (!HRWBwriteReg(link, HRWB_TCP_IP_ADDR, HRWB_scratch, sizeof(tIPaddr )))
00905 return false;
00906
00907 memcpy(HRWB_scratch, tmpPort, 2);
00908 if (!HRWBwriteReg(link, HRWB_TCP_PORT, HRWB_scratch, 2))
00909 return false;
00910
00911 writeDebugStreamLine("port: %2x %2x", HRWB_scratch[0], HRWB_scratch[1]);
00912
00913 if (!HRWBwriteReg(link, HRWB_TCP_TX_DATA, (tBigByteArray)tcprequest.TXData[0], tcprequest.TXDataLen))
00914 return false;
00915
00916 memcpy(HRWB_scratch, tcprequest.TXDataLen, 1);
00917 if (!HRWBwriteReg(link, HRWB_TCP_TX_LENGTH, HRWB_scratch, 1))
00918 return false;
00919
00920 if (!HRWBwriteRegMasked(link, HRWB_TCP_STATUS, HRWB_TCP_STATUS_SEND_REQ, HRWB_TCP_STATUS_SEND_REQ))
00921 return false;
00922
00923 while(!done) {
00924 if (!HRWBreadReg(link, HRWB_TCP_STATUS, 1))
00925 return false;
00926 status = HRWB_I2CReply[0];
00927
00928 if ((status & HRWB_TCP_STATUS_REQ_ERROR) == HRWB_TCP_STATUS_REQ_ERROR)
00929 {
00930 tcprequest.result_code = status;
00931 done = true;
00932 }
00933 else if ((status & HRWB_TCP_STATUS_REQ_DONE) == HRWB_TCP_STATUS_REQ_DONE)
00934 {
00935 tcprequest.result_code = 0;
00936 done = true;
00937 }
00938 wait1Msec(100);
00939 }
00940
00941 wait1Msec(100);
00942 if (!HRWBreadReg(link, HRWB_TCP_RX_LENGTH, 1))
00943 return false;
00944 tcprequest.RXDataLen = HRWB_I2CReply[0];
00945
00946 writeDebugStreamLine("res length: %d", tcprequest.RXDataLen);
00947 if (!HRWBreadBigReg(link, HRWB_TCP_RX_DATA, 32))
00948 return false;
00949
00950 memcpy(&tcprequest.RXData[0], &HRWB_HugeArray[0], sizeof(tBigByteArray));
00951
00952
00953 return true;
00954 }
00955
00956
00957
00958
00959
00960
00961
00962
00963 bool HRWBdoUDP(tSensors link, tUDPRequest &udprequest) {
00964 ubyte status = 0;
00965 bool done = false;
00966 HRWBreadReg(link, HRWB_UDP_STATUS, 1);
00967 status = HRWB_I2CReply[0];
00968 ubyte tmpsPort[2];
00969 ubyte tmpdPort[2];
00970
00971
00972 tmpsPort[0] = (udprequest.sport >> 8) & 0xFF;
00973 tmpsPort[1] = (udprequest.sport >> 0) & 0xFF;
00974
00975 tmpdPort[0] = (udprequest.dport >> 8) & 0xFF;
00976 tmpdPort[1] = (udprequest.dport >> 0) & 0xFF;
00977
00978 if (status & HRWB_UDP_STATUS_READY != HRWB_UDP_STATUS_READY)
00979 writeDebugStreamLine("NOT READY");
00980
00981 memcpy(HRWB_scratch, udprequest.IP[0], sizeof(tIPaddr));
00982 if (!HRWBwriteReg(link, HRWB_UDP_IP_ADDR, HRWB_scratch, sizeof(tIPaddr )))
00983 return false;
00984
00985 memcpy(HRWB_scratch, tmpsPort, 2);
00986 if (!HRWBwriteReg(link, HRWB_UDP_SPORT, HRWB_scratch, 2))
00987 return false;
00988
00989
00990 memcpy(HRWB_scratch, tmpdPort, 2);
00991 if (!HRWBwriteReg(link, HRWB_UDP_DPORT, HRWB_scratch, 2))
00992 return false;
00993
00994
00995 if (!HRWBwriteReg(link, HRWB_UDP_TX_DATA, (tBigByteArray)udprequest.TXData[0], udprequest.TXDataLen))
00996 return false;
00997
00998 memcpy(HRWB_scratch, udprequest.TXDataLen, 1);
00999 if (!HRWBwriteReg(link, HRWB_UDP_TX_LENGTH, HRWB_scratch, 1))
01000 return false;
01001
01002 if (!HRWBwriteRegMasked(link, HRWB_UDP_STATUS, HRWB_UDP_STATUS_SEND_REQ, HRWB_UDP_STATUS_SEND_REQ))
01003 return false;
01004
01005 while(!done) {
01006 if (!HRWBreadReg(link, HRWB_UDP_STATUS, 1))
01007 return false;
01008 status = HRWB_I2CReply[0];
01009
01010 if ((status & HRWB_UDP_STATUS_REQ_ERROR) == HRWB_UDP_STATUS_REQ_ERROR)
01011 {
01012 udprequest.result_code = status;
01013 done = true;
01014 }
01015 else if ((status & HRWB_UDP_STATUS_REQ_DONE) == HRWB_UDP_STATUS_REQ_DONE)
01016 {
01017 udprequest.result_code = status;
01018 done = true;
01019 }
01020 wait1Msec(100);
01021 }
01022
01023 wait1Msec(100);
01024 if (!HRWBreadReg(link, HRWB_UDP_RX_LENGTH, 1))
01025 return false;
01026 udprequest.RXDataLen = HRWB_I2CReply[0];
01027
01028
01029 if (!HRWBreadBigReg(link, HRWB_UDP_RX_DATA, 32))
01030 return false;
01031
01032 memcpy(&udprequest.RXData[0], &HRWB_HugeArray[0], sizeof(tBigByteArray));
01033
01034
01035 return true;
01036 }
01037
01038
01039 #endif // __HRWB_H__
01040
01041
01042
01043
01044
01045