nfc_app.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "nfc_app.h"
  19. #include "NT3H.h"
  20. #include <cJSON.h>
  21. #include "iot_nfc.h"
  22. char product[50],wifissid[50], wifipwd[50],deviceid[50], devicepwd[50];
  23. /**************************************************
  24. * get_devived_data
  25. * 获取WiFi的ssid和密钥及设备DevicesID和密钥
  26. * ***********************************************/
  27. uint8_t nfc_get_devived_data(void)
  28. {
  29. uint8_t memary_buf[16 * 15];
  30. cJSON *json, *jsonTemp; // *jsonArray,
  31. uint8_t jsonbuf[512] = {0};
  32. char jsonbuf_string[512] = {0};
  33. uint8_t payload_len = 0;
  34. uint8_t json_len = 0;
  35. NT3H1101_Read_Userpages(15, memary_buf);
  36. payload_len = memary_buf[4];
  37. json_len = payload_len - 3;
  38. //取出真实的json数据包,往后再偏移3位
  39. for (uint8_t i = 0; i < json_len; i++)
  40. {
  41. jsonbuf[i] = memary_buf[9 + i];
  42. }
  43. memset(jsonbuf_string, 0x00, sizeof(jsonbuf_string));
  44. sprintf(jsonbuf_string, "%s", jsonbuf);
  45. //解析json数据
  46. json = cJSON_Parse(jsonbuf_string);
  47. if (!json)
  48. {
  49. printf("Error before: [%s]\n", cJSON_GetErrorPtr());
  50. return -1;
  51. }
  52. else
  53. {
  54. jsonTemp = cJSON_GetObjectItem(json, "product");
  55. memset(product, 0, sizeof(product));
  56. snprintf(product, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring);
  57. jsonTemp = cJSON_GetObjectItem(json, "device_id");
  58. memset(deviceid, 0, sizeof(deviceid));
  59. snprintf(deviceid, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring);
  60. jsonTemp = cJSON_GetObjectItem(json, "secret");
  61. memset(devicepwd, 0, sizeof(devicepwd));
  62. snprintf(devicepwd, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring);
  63. jsonTemp = cJSON_GetObjectItem(json, "ssid");
  64. memset(wifissid, 0, sizeof(wifissid));
  65. snprintf(wifissid, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring);
  66. jsonTemp = cJSON_GetObjectItem(json, "pwd");
  67. memset(wifipwd, 0, sizeof(wifipwd));
  68. snprintf(wifipwd, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring);
  69. cJSON_Delete(json);
  70. free(json); // isequal
  71. }
  72. return 0;
  73. }
  74. int BOARD_InitNfc(void)
  75. {
  76. return 0;
  77. }
  78. int BOARD_GetNfcInfo(NfcInfo *info)
  79. {
  80. int ret = -1;
  81. if (info == NULL) {
  82. return ret;
  83. }
  84. if (nfc_get_devived_data() == 0){
  85. info->deviceID = (const char *)deviceid;
  86. info->devicePWD = (const char *)devicepwd;
  87. info->wifiSSID = (const char *)wifissid;
  88. info->wifiPWD = (const char *)wifipwd;
  89. ret = 0;
  90. }
  91. return ret;
  92. }