From 1388f26da5143cffaca157c9042fde618a265778 Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Tue, 22 Jan 2019 17:07:22 +0800 Subject: Reformat source code, remove WiFi in BLE settings --- src/main.cpp | 406 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 201 insertions(+), 205 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 381c12b..b115bc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,6 @@ // BLE #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" -#define WIFI_CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" #define OWNER_CHARACTERISTIC_UUID "f925981a-7be4-45af-9b61-23979364a0e2" #define CONTRACT_ADDRESSS_CHARACTERISTIC_UUID "25f656a8-89db-4d94-a5d3-1da16c10fa3e" @@ -30,11 +29,11 @@ WebSocketsClient webSocket; Web3 web3(RPC_HOST, RPC_PATH); enum State { - STATE_INIT = 0, - STATE_WIFI_CONNECTED, - STATE_CONTRACT_DEPLOYED, - STATE_WS_CONNECTED, - STATE_LOGS_SUBSCRIBED, + STATE_INIT = 0, + STATE_WIFI_CONNECTED, + STATE_CONTRACT_DEPLOYED, + STATE_WS_CONNECTED, + STATE_LOGS_SUBSCRIBED, }; State state; @@ -45,241 +44,238 @@ std::string ownerAddress; BLECharacteristic *pContractAddressCharacteristic; void on_websocket_event(WStype_t type, uint8_t * payload, size_t length) { - switch (type) { - case WStype_CONNECTED: - { - Serial.println("Websocket connected."); - - string req = std::string("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_subscribe\",\"params\":[\"logs\",{\"address\":\"") + - contractAddress + - std::string("\",\"topics\":[\"0xb87f936dd029fdc50266e531c100fb6f7e2b1f385e524eb8a736753539fc25e8\"]}]}"); - - webSocket.sendTXT(req.c_str()); - break; - } - case WStype_DISCONNECTED: - Serial.println("Websocket disconnected."); - break; - case WStype_TEXT: - { - int status; - if (state < STATE_LOGS_SUBSCRIBED) { - Serial.println((const char*) payload); - state = STATE_LOGS_SUBSCRIBED; - break; - } - Serial.println((const char*)payload); - cJSON *root = NULL, *params = NULL, *result = NULL, *data = NULL; - root = cJSON_Parse((const char*)payload); - if (root == NULL) { - goto cleanup; - } - params = cJSON_GetObjectItem(root, "params"); - if (params == NULL) { - Serial.println("get params error"); - goto cleanup; - } - result = cJSON_GetObjectItem(params, "result"); - if (result == NULL) { - Serial.println("get data error"); - goto cleanup; - } - data = cJSON_GetObjectItem(result, "data"); - if (data == NULL) { - Serial.println("get data error"); - goto cleanup; - } - status = strtol(data->valuestring, nullptr, 16); - Serial.printf("Status: %d\n", status); - digitalWrite(LED_PIN, status); + switch (type) { + case WStype_CONNECTED: + { + Serial.println("Websocket connected."); + + string req = std::string("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_subscribe\",\"params\":[\"logs\",{\"address\":\"") + + contractAddress + + std::string("\",\"topics\":[\"0xb87f936dd029fdc50266e531c100fb6f7e2b1f385e524eb8a736753539fc25e8\"]}]}"); + + webSocket.sendTXT(req.c_str()); + break; + } + case WStype_DISCONNECTED: + Serial.println("Websocket disconnected."); + break; + case WStype_TEXT: + { + int status; + if (state < STATE_LOGS_SUBSCRIBED) { + Serial.println((const char*) payload); + state = STATE_LOGS_SUBSCRIBED; + break; + } + Serial.println((const char*)payload); + cJSON *root = NULL, *params = NULL, *result = NULL, *data = NULL; + root = cJSON_Parse((const char*)payload); + if (root == NULL) { + goto cleanup; + } + params = cJSON_GetObjectItem(root, "params"); + if (params == NULL) { + Serial.println("get params error"); + goto cleanup; + } + result = cJSON_GetObjectItem(params, "result"); + if (result == NULL) { + Serial.println("get data error"); + goto cleanup; + } + data = cJSON_GetObjectItem(result, "data"); + if (data == NULL) { + Serial.println("get data error"); + goto cleanup; + } + status = strtol(data->valuestring, nullptr, 16); + Serial.printf("Status: %d\n", status); + digitalWrite(LED_PIN, status); cleanup: - if (root != NULL) - cJSON_free(root); - if (params != NULL) - cJSON_free(params); - if (result != NULL) - cJSON_free(result); - if (data != NULL) - cJSON_free(data); - break; - } - default: - Serial.printf("unhandled event: %d\n", type); - } + if (root != NULL) + cJSON_free(root); + if (params != NULL) + cJSON_free(params); + if (result != NULL) + cJSON_free(result); + if (data != NULL) + cJSON_free(data); + break; + } + default: + Serial.printf("unhandled event: %d\n", type); + } } std::string deploy_contract(std::string owner) { - Contract contract(&web3, ""); - contract.SetPrivateKey(PRIVATE_KEY); - - std::string address = MY_ADDRESS; - uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&address); - unsigned long long gasPriceVal = 20000000000ULL; - uint32_t gasLimitVal = 5000000; - std::string value; - std::string emptyString; - - value = Util::ConvertEthToWei(1); - std::string result = contract.SendTransaction(nonceVal, gasPriceVal, gasLimitVal, &ownerAddress, &value, &emptyString); - Serial.println(result.c_str()); - - std::string data = string(CONTRACT_BYTECODE) + "000000000000000000000000" + owner.substr(2); - value = "0"; - result = contract.SendTransaction(nonceVal + 1, gasPriceVal, gasLimitVal, &emptyString, &value, &data); - Serial.println(result.c_str()); - - string transactionHash = web3.getString(&result); - Serial.printf("TxHash: %s\n", transactionHash.c_str()); - - for (int i = 0; i < 7; i++) { - string contractAddress = web3.EthGetDeployedContractAddress(&transactionHash); - if (contractAddress.length() != 0) - return contractAddress; - delay(1000); - } - return ""; + Contract contract(&web3, ""); + contract.SetPrivateKey(PRIVATE_KEY); + + std::string address = MY_ADDRESS; + uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&address); + unsigned long long gasPriceVal = 20000000000ULL; + uint32_t gasLimitVal = 5000000; + std::string value; + std::string emptyString; + + value = Util::ConvertEthToWei(1); + std::string result = contract.SendTransaction(nonceVal, gasPriceVal, gasLimitVal, &ownerAddress, &value, &emptyString); + Serial.println(result.c_str()); + + std::string data = string(CONTRACT_BYTECODE) + "000000000000000000000000" + owner.substr(2); + value = "0"; + result = contract.SendTransaction(nonceVal + 1, gasPriceVal, gasLimitVal, &emptyString, &value, &data); + Serial.println(result.c_str()); + + string transactionHash = web3.getString(&result); + Serial.printf("TxHash: %s\n", transactionHash.c_str()); + + for (int i = 0; i < 7; i++) { + string contractAddress = web3.EthGetDeployedContractAddress(&transactionHash); + if (contractAddress.length() != 0) + return contractAddress; + delay(1000); + } + return ""; } void post_wifi_connect() { - Serial.printf("Deploying contract, setting owner to %s\n", ownerAddress.c_str()); - contractAddress = deploy_contract(ownerAddress); - if (contractAddress.length() == 0) { - Serial.println("Contract deployment failed."); - return; - } - Serial.printf("Contract deployed: %s\n", contractAddress.c_str()); - - pContractAddressCharacteristic->setValue(contractAddress.c_str()); - state = STATE_CONTRACT_DEPLOYED; - Serial.printf("done"); + if (ownerAddress.length() == 0) { + return; + } + Serial.printf("Deploying contract, setting owner to %s\n", ownerAddress.c_str()); + contractAddress = deploy_contract(ownerAddress); + if (contractAddress.length() == 0) { + Serial.println("Contract deployment failed."); + return; + } + Serial.printf("Contract deployed: %s\n", contractAddress.c_str()); + + pContractAddressCharacteristic->setValue(contractAddress.c_str()); + state = STATE_CONTRACT_DEPLOYED; } void post_contract_deployment() { - Contract contract(&web3, contractAddress.c_str()); - string param = contract.SetupContractData("powered()"); - string result = contract.ViewCall(¶m); - digitalWrite(LED_PIN, web3.getInt(&result)); - - webSocket.begin("testnet.dexon.org", 8546); - webSocket.onEvent(on_websocket_event); - state = STATE_WS_CONNECTED; + Contract contract(&web3, contractAddress.c_str()); + string param = contract.SetupContractData("powered()"); + string result = contract.ViewCall(¶m); + digitalWrite(LED_PIN, web3.getInt(&result)); + + webSocket.begin("testnet.dexon.org", 8546); + webSocket.onEvent(on_websocket_event); + state = STATE_WS_CONNECTED; } void setup_wifi(const char* ssid, const char* password) { - if (WiFi.status() == WL_CONNECTED) { - return; - } - - Serial.println(); - Serial.print("Connecting to "); - Serial.println(ssid); - - if (WiFi.status() != WL_CONNECTED) { - WiFi.persistent(false); - WiFi.mode(WIFI_OFF); - WiFi.mode(WIFI_STA); - - WiFi.begin(ssid, password); - } - - wificounter = 0; - while (WiFi.status() != WL_CONNECTED && wificounter < 10) { - delay(500); - Serial.print("."); - wificounter++; - } - - if (wificounter >= 10) { - Serial.println("Can not connect"); - return; - } - - delay(10); - - Serial.println(""); - Serial.println("WiFi connected."); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - - state = STATE_WIFI_CONNECTED; + if (WiFi.status() == WL_CONNECTED) { + return; + } + + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + if (WiFi.status() != WL_CONNECTED) { + WiFi.persistent(false); + WiFi.mode(WIFI_OFF); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, password); + } + + wificounter = 0; + while (WiFi.status() != WL_CONNECTED && wificounter < 10) { + delay(500); + Serial.print("."); + wificounter++; + } + + if (wificounter >= 10) { + Serial.println("Can not connect"); + return; + } + + delay(10); + + Serial.println(""); + Serial.println("WiFi connected."); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + state = STATE_WIFI_CONNECTED; } class WiFiCharacteristicCallback : public BLECharacteristicCallbacks { - virtual void onWrite(BLECharacteristic* pCharacteristic) { - std::string wifiSettings = pCharacteristic->getValue(); - int pos = wifiSettings.find_first_of(" "); - if (pos == std::string::npos) { - return; - } - std::string ssid = wifiSettings.substr(0, pos); - std::string password = wifiSettings.substr(pos + 1); - Serial.println(ssid.c_str()); - Serial.println(password.c_str()); - setup_wifi(ssid.c_str(), password.c_str()); + virtual void onWrite(BLECharacteristic* pCharacteristic) { + std::string wifiSettings = pCharacteristic->getValue(); + int pos = wifiSettings.find_first_of(" "); + if (pos == std::string::npos) { + return; } + std::string ssid = wifiSettings.substr(0, pos); + std::string password = wifiSettings.substr(pos + 1); + Serial.println(ssid.c_str()); + Serial.println(password.c_str()); + setup_wifi(ssid.c_str(), password.c_str()); + } }; class OwnerCharacteristicCallback : public BLECharacteristicCallbacks { - virtual void onWrite(BLECharacteristic* pCharacteristic) { - ownerAddress = pCharacteristic->getValue(); - } + virtual void onWrite(BLECharacteristic* pCharacteristic) { + ownerAddress = pCharacteristic->getValue(); + } }; void setup_bluetooth() { - BLEDevice::init("DEXON IoT Core"); - - BLEServer *pServer = BLEDevice::createServer(); - BLEService *pService = pServer->createService(SERVICE_UUID); - BLECharacteristic *c = pService->createCharacteristic( - WIFI_CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_READ | - BLECharacteristic::PROPERTY_WRITE); - c->setCallbacks(new WiFiCharacteristicCallback()); - - c = pService->createCharacteristic( - OWNER_CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_READ | - BLECharacteristic::PROPERTY_WRITE); - c->setCallbacks(new OwnerCharacteristicCallback()); - - pContractAddressCharacteristic = pService->createCharacteristic( - CONTRACT_ADDRESSS_CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_READ); - - pService->start(); - BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); - pAdvertising->addServiceUUID(SERVICE_UUID); - pAdvertising->setScanResponse(true); - pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue - pAdvertising->setMinPreferred(0x12); - BLEDevice::startAdvertising(); - Serial.println("BLE advertisement started."); + BLEDevice::init("DEXON IoT Core"); + + BLEServer *pServer = BLEDevice::createServer(); + BLEService *pService = pServer->createService(SERVICE_UUID); + + BLECharacteristic* c = pService->createCharacteristic( + OWNER_CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE); + c->setCallbacks(new OwnerCharacteristicCallback()); + + pContractAddressCharacteristic = pService->createCharacteristic( + CONTRACT_ADDRESSS_CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ); + + pService->start(); + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(SERVICE_UUID); + pAdvertising->setScanResponse(true); + pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue + pAdvertising->setMinPreferred(0x12); + BLEDevice::startAdvertising(); + Serial.println("BLE advertisement started."); } void setup() { - Serial.begin(115200); + Serial.begin(115200); - pinMode(LED_PIN, OUTPUT); + pinMode(LED_PIN, OUTPUT); - //setup_wifi("COBINHOOD_Guest", "COB0921592018"); - setup_bluetooth(); - //ownerAddress = "0x32528352352B73fAE48AbB05945EA457797D8bDC"; + setup_wifi("COBINHOOD_Guest", "COB0921592018"); + setup_bluetooth(); + //ownerAddress = "0x32528352352B73fAE48AbB05945EA457797D8bDC"; } void loop() { - switch (state) { + switch (state) { case STATE_INIT: - return; + return; case STATE_WIFI_CONNECTED: - post_wifi_connect(); - return; + post_wifi_connect(); + return; case STATE_CONTRACT_DEPLOYED: - post_contract_deployment(); - return; + post_contract_deployment(); + return; case STATE_WS_CONNECTED: case STATE_LOGS_SUBSCRIBED: default: - webSocket.loop(); - } + webSocket.loop(); + } } -- cgit v1.2.3