二期c++接口

Dependencies:   EthernetInterface mbed-rtos mbed uniqueCPUID

Fork of bcsdk by Heng Well

Committer:
MrAI
Date:
Wed May 16 13:46:01 2018 +0000
Revision:
12:1b24ea479a59
Parent:
11:6d9cefb14158
Child:
13:e77c7ae3a285
??14???

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsteiert 0:28bc7ce77e20 1 #include "mbed.h"
gsteiert 0:28bc7ce77e20 2 #include "EthernetInterface.h"
MrAI 11:6d9cefb14158 3 //#include "SDFileSystem.h"
gsteiert 0:28bc7ce77e20 4 #include <stdio.h>
webmaster 9:d7468574ef8f 5 #include "uniqueCPUID.h"
webmaster 8:f2a567ee3a46 6
webmaster 8:f2a567ee3a46 7 #include <iostream>
webmaster 8:f2a567ee3a46 8 #include <string>
webmaster 8:f2a567ee3a46 9 #include "UnitTest.h"
webmaster 8:f2a567ee3a46 10 //#include "BlockChain.h"
webmaster 8:f2a567ee3a46 11 #include "MockHsm.h"
webmaster 10:aabd720e632c 12 #include "Asset.h"
webmaster 10:aabd720e632c 13 #include "Transaction.h"
webmaster 10:aabd720e632c 14 #include "Account.h"
webmaster 10:aabd720e632c 15 #include "Key.h"
MrAI 12:1b24ea479a59 16 #include "Payment.h"
MrAI 12:1b24ea479a59 17 #include "ServerInfo.h"
MrAI 12:1b24ea479a59 18 #include "Order.h"
webmaster 8:f2a567ee3a46 19
webmaster 8:f2a567ee3a46 20 using namespace std;
gsteiert 0:28bc7ce77e20 21
gsteiert 0:28bc7ce77e20 22 #define HTTPD_SERVER_PORT 80
gsteiert 0:28bc7ce77e20 23 #define HTTPD_MAX_REQ_LENGTH 1023
gsteiert 0:28bc7ce77e20 24 #define HTTPD_MAX_HDR_LENGTH 255
gsteiert 0:28bc7ce77e20 25 #define HTTPD_MAX_FNAME_LENGTH 127
gsteiert 3:4f71a37a1ed2 26 #define HTTPD_MAX_DNAME_LENGTH 127
gsteiert 3:4f71a37a1ed2 27
webmaster 10:aabd720e632c 28 #define DEBUG_LOG 1
webmaster 10:aabd720e632c 29
webmaster 8:f2a567ee3a46 30 const char* ECHO_SERVER_ADDRESS = "192.168.31.185";
webmaster 8:f2a567ee3a46 31 const int ECHO_SERVER_PORT = 10003;
webmaster 8:f2a567ee3a46 32
gsteiert 3:4f71a37a1ed2 33 Serial uart(USBTX, USBRX);
gsteiert 0:28bc7ce77e20 34
gsteiert 6:ca17ce165ebc 35 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
gsteiert 5:27b09a0f3fb6 36 //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
MrAI 11:6d9cefb14158 37 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
gsteiert 3:4f71a37a1ed2 38
gsteiert 0:28bc7ce77e20 39 EthernetInterface eth;
gsteiert 0:28bc7ce77e20 40 TCPSocketServer server;
gsteiert 0:28bc7ce77e20 41 TCPSocketConnection client;
webmaster 8:f2a567ee3a46 42 TCPSocketConnection my_client;
gsteiert 0:28bc7ce77e20 43
gsteiert 0:28bc7ce77e20 44 char buffer[HTTPD_MAX_REQ_LENGTH+1];
gsteiert 0:28bc7ce77e20 45 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
gsteiert 3:4f71a37a1ed2 46 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
gsteiert 3:4f71a37a1ed2 47 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
gsteiert 0:28bc7ce77e20 48 char *uristr;
gsteiert 0:28bc7ce77e20 49 char *eou;
gsteiert 0:28bc7ce77e20 50 char *qrystr;
gsteiert 0:28bc7ce77e20 51
gsteiert 0:28bc7ce77e20 52 FILE *fp;
gsteiert 0:28bc7ce77e20 53 int rdCnt;
gsteiert 0:28bc7ce77e20 54
webmaster 8:f2a567ee3a46 55 // function declation
webmaster 8:f2a567ee3a46 56 int test_socket_client(void);
webmaster 8:f2a567ee3a46 57 int socket_init();
webmaster 8:f2a567ee3a46 58 int socket_fini();
webmaster 8:f2a567ee3a46 59
webmaster 10:aabd720e632c 60
webmaster 9:d7468574ef8f 61 void test_uuid()
webmaster 9:d7468574ef8f 62 {
webmaster 9:d7468574ef8f 63 char pUUID[128] = {0};
webmaster 9:d7468574ef8f 64 getUniqueIDAsStr(pUUID);
webmaster 9:d7468574ef8f 65 printf("CPUID = %s \n",pUUID);
webmaster 9:d7468574ef8f 66 //printUniqueId(uart);
webmaster 9:d7468574ef8f 67 }
webmaster 8:f2a567ee3a46 68
webmaster 10:aabd720e632c 69 int test_MockHsm()
webmaster 10:aabd720e632c 70 {
webmaster 10:aabd720e632c 71 MockHsm mh;
webmaster 10:aabd720e632c 72 mh.setAlias("t_key_6");
webmaster 10:aabd720e632c 73 cout<<mh.getAlias()<<endl;
webmaster 10:aabd720e632c 74 cout<<mh.createKey()<<endl;
webmaster 10:aabd720e632c 75 cout<< mh.listKeys() <<endl;
webmaster 10:aabd720e632c 76 return 0;
webmaster 10:aabd720e632c 77 }
webmaster 10:aabd720e632c 78
webmaster 10:aabd720e632c 79 int test_Asset()
webmaster 10:aabd720e632c 80 {
webmaster 10:aabd720e632c 81 Asset asset;
webmaster 10:aabd720e632c 82 asset.setAlias("t_asset_6");
webmaster 10:aabd720e632c 83
webmaster 10:aabd720e632c 84 cout<< asset.createAsset() << endl;
webmaster 10:aabd720e632c 85 cout<< asset.listAssets() << endl;
webmaster 10:aabd720e632c 86
webmaster 10:aabd720e632c 87
webmaster 10:aabd720e632c 88 return 0;
webmaster 10:aabd720e632c 89 }
webmaster 10:aabd720e632c 90
webmaster 10:aabd720e632c 91 int test_Account()
webmaster 10:aabd720e632c 92 {
webmaster 10:aabd720e632c 93 Key key;
webmaster 10:aabd720e632c 94 key.setAlias("t_key");
webmaster 10:aabd720e632c 95 key.listKeys();
webmaster 10:aabd720e632c 96
webmaster 10:aabd720e632c 97 Account act(key);
webmaster 10:aabd720e632c 98 act.setAlias("t_acc_8");
webmaster 10:aabd720e632c 99 act.createAccount();
webmaster 10:aabd720e632c 100 act.listAccounts();
webmaster 10:aabd720e632c 101
webmaster 10:aabd720e632c 102 return 0;
webmaster 10:aabd720e632c 103 }
webmaster 10:aabd720e632c 104
webmaster 10:aabd720e632c 105 int test_transaction(string type)
webmaster 10:aabd720e632c 106 {
webmaster 10:aabd720e632c 107 Transaction ts;
webmaster 10:aabd720e632c 108 //cout << ts.buildTransaction() << endl;
webmaster 10:aabd720e632c 109 ts.buildTransaction(type);
webmaster 10:aabd720e632c 110 ts.signTransaction();
webmaster 10:aabd720e632c 111 ts.submitTransaction();
webmaster 10:aabd720e632c 112 ts.listTransactions();
webmaster 10:aabd720e632c 113 return 0;
webmaster 10:aabd720e632c 114 }
webmaster 10:aabd720e632c 115
webmaster 10:aabd720e632c 116 int test_transaction()
webmaster 10:aabd720e632c 117 {
webmaster 10:aabd720e632c 118 vector<Actions> vActions;
webmaster 10:aabd720e632c 119 Actions act;
webmaster 10:aabd720e632c 120 MbedJSONValue reference_data;
webmaster 10:aabd720e632c 121
webmaster 10:aabd720e632c 122 act.accounts_alias = "t_acc_1";
webmaster 10:aabd720e632c 123 act.amount = 51;
webmaster 10:aabd720e632c 124 act.asset_alias = "t_asset";
webmaster 10:aabd720e632c 125 act.reference_data = reference_data;
webmaster 10:aabd720e632c 126 act.type = "spend_account";
webmaster 10:aabd720e632c 127 vActions.push_back(act);
webmaster 10:aabd720e632c 128
webmaster 10:aabd720e632c 129 Actions act1 = {"t_acc_2", "t_asset", 49, reference_data, "spend_account" };
webmaster 10:aabd720e632c 130 Actions act2 = {"tom", "t_asset", 80, reference_data, "control_account"};
webmaster 10:aabd720e632c 131 Actions act3 = {"Jerry", "t_asset", 20, reference_data, "control_account"};
webmaster 10:aabd720e632c 132
webmaster 10:aabd720e632c 133 vActions.push_back(act1);
webmaster 10:aabd720e632c 134 vActions.push_back(act2);
webmaster 10:aabd720e632c 135 vActions.push_back(act3);
webmaster 10:aabd720e632c 136 printf("=============2========\r\n");
webmaster 10:aabd720e632c 137 Transaction ts;
webmaster 10:aabd720e632c 138 ts.buildTransaction(vActions);
webmaster 10:aabd720e632c 139 ts.signTransaction();
webmaster 10:aabd720e632c 140 ts.submitTransaction();
webmaster 10:aabd720e632c 141 ts.listTransactions();
webmaster 10:aabd720e632c 142
webmaster 10:aabd720e632c 143 return 0;
webmaster 10:aabd720e632c 144 }
webmaster 10:aabd720e632c 145
webmaster 10:aabd720e632c 146
MrAI 12:1b24ea479a59 147
MrAI 12:1b24ea479a59 148 int sd_account()
MrAI 12:1b24ea479a59 149 {
MrAI 12:1b24ea479a59 150 string balance = "";
MrAI 12:1b24ea479a59 151 Account act;
MrAI 12:1b24ea479a59 152 balance = act.getBalance();
MrAI 12:1b24ea479a59 153 cout << "Extract json content from http message:"<<balance.c_str() << endl;
MrAI 12:1b24ea479a59 154 if (balance.empty())
MrAI 12:1b24ea479a59 155 {
MrAI 12:1b24ea479a59 156 return -1;
MrAI 12:1b24ea479a59 157 }
MrAI 12:1b24ea479a59 158 // decode content json
MrAI 12:1b24ea479a59 159 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 160 //string err = parse(objJson, balance.c_str());
MrAI 12:1b24ea479a59 161 //int iLedger = objJson["ledger"].get<int>();
MrAI 12:1b24ea479a59 162 //bool bSuccess = objJson["success"].get<bool>();
MrAI 12:1b24ea479a59 163 //int iCurrenciesNum = objJson["balances"].size();
MrAI 12:1b24ea479a59 164 /*for (int i = 0;i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 165 {
MrAI 12:1b24ea479a59 166 string value = objJson["balances"][i]["value"].get<string>();
MrAI 12:1b24ea479a59 167 string currency = objJson["balances"][i]["currency"].get<string>();
MrAI 12:1b24ea479a59 168 string counterparty = objJson["balances"][i]["counterparty"].get<string>();
MrAI 12:1b24ea479a59 169 cout << i << ":" << currency << "--" << value << "--" << counterparty << endl;
MrAI 12:1b24ea479a59 170 }*/
MrAI 12:1b24ea479a59 171 string err = act.getParseMbedJson(objJson, balance.c_str());
MrAI 12:1b24ea479a59 172 int iLedger = act.getLedger(objJson);
MrAI 12:1b24ea479a59 173 bool bSuccess = act.getRequestRet(objJson);
MrAI 12:1b24ea479a59 174 int iCurrenciesNum = act.getBalanceSize(objJson,"balances");
MrAI 12:1b24ea479a59 175 for (int i = 0; i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 176 {
MrAI 12:1b24ea479a59 177 string value = act.getBalanceArray(objJson, "balances", i, "value");
MrAI 12:1b24ea479a59 178 string currency = act.getBalanceArray(objJson, "balances", i, "currency");
MrAI 12:1b24ea479a59 179 string counterparty = act.getBalanceArray(objJson, "balances", i, "counterparty");
MrAI 12:1b24ea479a59 180 cout << "The " << i <<" record:" << endl << "value:" << value << endl << "currency:"<<currency<<endl
MrAI 12:1b24ea479a59 181 << "counterparty:" << counterparty << endl;
MrAI 12:1b24ea479a59 182 }
MrAI 12:1b24ea479a59 183 return 0;
MrAI 12:1b24ea479a59 184 }
MrAI 12:1b24ea479a59 185
MrAI 12:1b24ea479a59 186 int sd_wallet()
MrAI 12:1b24ea479a59 187 {
MrAI 12:1b24ea479a59 188 string strJson = "";
MrAI 12:1b24ea479a59 189 Account act;
MrAI 12:1b24ea479a59 190 strJson = act.getWallet ();
MrAI 12:1b24ea479a59 191 cout << "Extract json content from http message:"<<strJson.c_str() << endl;
MrAI 12:1b24ea479a59 192 if (strJson.empty())
MrAI 12:1b24ea479a59 193 {
MrAI 12:1b24ea479a59 194 return -1;
MrAI 12:1b24ea479a59 195 }
MrAI 12:1b24ea479a59 196 // decode content json
MrAI 12:1b24ea479a59 197 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 198 // string err = parse(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 199 // bool bSuccess = objJson["success"].get<bool>();
MrAI 12:1b24ea479a59 200 // if (bSuccess)
MrAI 12:1b24ea479a59 201 // {
MrAI 12:1b24ea479a59 202 // string address = objJson["wallet"]["address"].get<string>();
MrAI 12:1b24ea479a59 203 // string secret = objJson["wallet"]["secret"].get<string>();
MrAI 12:1b24ea479a59 204 // cout << "wallet:" << address << "--" << secret << endl;
MrAI 12:1b24ea479a59 205 // }
MrAI 12:1b24ea479a59 206 string err = act.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 207 bool bSuccess = act.getRequestRet(objJson);
MrAI 12:1b24ea479a59 208 if (bSuccess)
MrAI 12:1b24ea479a59 209 {
MrAI 12:1b24ea479a59 210 string address1 = act.getKeyValue(objJson, "wallet", "address");
MrAI 12:1b24ea479a59 211 string secret1 = act.getKeyValue(objJson, "wallet", "secret");
MrAI 12:1b24ea479a59 212 cout << "address:" << address1 << endl << "secret:" << secret1 << endl;
MrAI 12:1b24ea479a59 213 }
MrAI 12:1b24ea479a59 214
MrAI 12:1b24ea479a59 215 return 0;
MrAI 12:1b24ea479a59 216 }
MrAI 12:1b24ea479a59 217
MrAI 12:1b24ea479a59 218 int sd_postpayment()//支付请求
MrAI 12:1b24ea479a59 219 {
MrAI 12:1b24ea479a59 220 string strJson = "";
MrAI 12:1b24ea479a59 221 Payment pay;
MrAI 12:1b24ea479a59 222 MbedJSONValue mbJson;
MrAI 12:1b24ea479a59 223 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 224 string secret = "sh7dqVn8mhg6BqGfAQyaYVUwK2zkR";
MrAI 12:1b24ea479a59 225 string src_acc = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 226 string dst_acc = "69Hu8QsnEZUP4f3AVYSW7rkpLVm6DAEq9y";
MrAI 12:1b24ea479a59 227 string amount = "20";
MrAI 12:1b24ea479a59 228 string MemoType = "memos";
MrAI 12:1b24ea479a59 229 string MemoData = "memos";
MrAI 12:1b24ea479a59 230 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 231 string strUrl = "/v1/accounts/payments/";
MrAI 12:1b24ea479a59 232 pay.SetJsonBody(mbJson, secret, src_acc, dst_acc, amount, MemoType, MemoData);
MrAI 12:1b24ea479a59 233 strJson = pay.PostPayment(mbJson, strUrl, strWallet,"");
MrAI 12:1b24ea479a59 234 if (strJson.empty())
MrAI 12:1b24ea479a59 235 {
MrAI 12:1b24ea479a59 236 return -1;
MrAI 12:1b24ea479a59 237 }
MrAI 12:1b24ea479a59 238 string err = pay.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 239 bool bSuccess = pay.getRequestRet(objJson);
MrAI 12:1b24ea479a59 240 if (bSuccess)
MrAI 12:1b24ea479a59 241 {
MrAI 12:1b24ea479a59 242 string hash = pay.getKeyValue(objJson, "hash");
MrAI 12:1b24ea479a59 243 string status_url = pay.getKeyValue(objJson, "status_url");
MrAI 12:1b24ea479a59 244 cout << "hash:" << hash << endl;
MrAI 12:1b24ea479a59 245 cout << "status_url:" << status_url << endl;
MrAI 12:1b24ea479a59 246 }
MrAI 12:1b24ea479a59 247 return 0;
MrAI 12:1b24ea479a59 248 }
MrAI 12:1b24ea479a59 249
MrAI 12:1b24ea479a59 250 int sd_getpaymentinfo()
MrAI 12:1b24ea479a59 251 {
MrAI 12:1b24ea479a59 252 Payment pay;
MrAI 12:1b24ea479a59 253 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 254 string strJson = "";
MrAI 12:1b24ea479a59 255 string strWallet = "69Hu8QsnEZUP4f3AVYSW7rkpLVm6DAEq9y";
MrAI 12:1b24ea479a59 256 string strUrl = "/v1/accounts/payments/";
MrAI 12:1b24ea479a59 257 string strHash = "EF252979355F2666486F69E9C96D137265214DE65B14E1136ECCDCE84F09AD11";
MrAI 12:1b24ea479a59 258 strJson = pay.GetPaymentInfo(strUrl, strWallet, strHash);
MrAI 12:1b24ea479a59 259 if (strJson.empty())
MrAI 12:1b24ea479a59 260 {
MrAI 12:1b24ea479a59 261 return -1;
MrAI 12:1b24ea479a59 262 }
MrAI 12:1b24ea479a59 263 string err = pay.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 264 bool bSuccess = pay.getRequestRet(objJson);
MrAI 12:1b24ea479a59 265 if (bSuccess)
MrAI 12:1b24ea479a59 266 {
MrAI 12:1b24ea479a59 267 string hash = pay.getKeyValue(objJson, "hash");
MrAI 12:1b24ea479a59 268 string ledger = pay.getKeyValue(objJson, "ledger");
MrAI 12:1b24ea479a59 269 string state = pay.getKeyValue(objJson, "state");
MrAI 12:1b24ea479a59 270 string source_account = pay.getKeyValue(objJson, "source_account");
MrAI 12:1b24ea479a59 271 string dst_account = pay.getKeyValue(objJson, "destination_account");
MrAI 12:1b24ea479a59 272 string amount_cur = pay.getKeyValue(objJson, "amount", "currency");
MrAI 12:1b24ea479a59 273 string value = pay.getKeyValue(objJson, "amount", "value");
MrAI 12:1b24ea479a59 274 string amount_issuer = pay.getKeyValue(objJson, "amount", "issuer");
MrAI 12:1b24ea479a59 275 string currency = pay.getKeyValue(objJson, "currency");
MrAI 12:1b24ea479a59 276 string issuer = pay.getKeyValue(objJson, "issuer");
MrAI 12:1b24ea479a59 277 string direction = pay.getKeyValue(objJson, "direction");
MrAI 12:1b24ea479a59 278 string timestamp = pay.getKeyValue(objJson, "timestamp");
MrAI 12:1b24ea479a59 279 string fee = pay.getKeyValue(objJson, "fee");
MrAI 12:1b24ea479a59 280 cout << "hash:" << hash << " ledger:" << ledger << " state:" << state
MrAI 12:1b24ea479a59 281 << " source_account:" << source_account << " dst_account:" << dst_account
MrAI 12:1b24ea479a59 282 << " amount_cur:" << amount_cur << " value:" << value << " amount_issuer:" << amount_issuer
MrAI 12:1b24ea479a59 283 << " currency:" << currency << " issuer" << issuer << " direction" << direction
MrAI 12:1b24ea479a59 284 << " timestamp:" << timestamp << " fee" << fee;
MrAI 12:1b24ea479a59 285 int iCurrenciesNum = pay.getBalanceSize(objJson, "memos");
MrAI 12:1b24ea479a59 286 for (int i = 0; i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 287 {
MrAI 12:1b24ea479a59 288 string memo_type = pay.getBalanceArray(objJson, "memos", i, "memo_type");
MrAI 12:1b24ea479a59 289 string memo_data = pay.getBalanceArray(objJson, "memos", i, "memo_data");
MrAI 12:1b24ea479a59 290 cout << i << ":" << memo_type << "--" << memo_data << endl;
MrAI 12:1b24ea479a59 291 }
MrAI 12:1b24ea479a59 292
MrAI 12:1b24ea479a59 293 }
MrAI 12:1b24ea479a59 294 return 0;
MrAI 12:1b24ea479a59 295 }
MrAI 12:1b24ea479a59 296
MrAI 12:1b24ea479a59 297 int sd_gettransactions()
MrAI 12:1b24ea479a59 298 {
MrAI 12:1b24ea479a59 299 Payment pay;
MrAI 12:1b24ea479a59 300 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 301 string strJson = "";
MrAI 12:1b24ea479a59 302 string strHash = "EF252979355F2666486F69E9C96D137265214DE65B14E1136ECCDCE84F09AD11";
MrAI 12:1b24ea479a59 303 string strUrl = "/v1/transactions/";
MrAI 12:1b24ea479a59 304 strJson = pay.GetPaymentInfo(strUrl,"", strHash);
MrAI 12:1b24ea479a59 305 if (strJson.empty())
MrAI 12:1b24ea479a59 306 {
MrAI 12:1b24ea479a59 307 return -1;
MrAI 12:1b24ea479a59 308 }
MrAI 12:1b24ea479a59 309 string err = pay.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 310 bool bSuccess = pay.getRequestRet(objJson);
MrAI 12:1b24ea479a59 311 if (bSuccess)
MrAI 12:1b24ea479a59 312 {
MrAI 12:1b24ea479a59 313 string TransactionType = pay.getKeyValue(objJson, "transaction", "TransactionType");
MrAI 12:1b24ea479a59 314 int Flags = pay.getKeyValueInt(objJson, "transaction", "Flags");
MrAI 12:1b24ea479a59 315 int Sequence = pay.getKeyValueInt(objJson, "transaction", "Sequence");
MrAI 12:1b24ea479a59 316 int LastLedgerSequence = pay.getKeyValueInt(objJson, "transaction", "LastLedgerSequence");
MrAI 12:1b24ea479a59 317 string Amount = pay.getKeyValue(objJson, "transaction", "Amount");
MrAI 12:1b24ea479a59 318 string Fee = pay.getKeyValue(objJson, "transaction", "Fee");
MrAI 12:1b24ea479a59 319 string SigningPubKey = pay.getKeyValue(objJson, "transaction", "SigningPubKey");
MrAI 12:1b24ea479a59 320 string TxnSignature = pay.getKeyValue(objJson, "transaction", "TxnSignature");
MrAI 12:1b24ea479a59 321 string Account = pay.getKeyValue(objJson, "transaction", "Account");
MrAI 12:1b24ea479a59 322 string Destination = pay.getKeyValue(objJson, "transaction", "Destination");
MrAI 12:1b24ea479a59 323 int iCurrenciesNum = pay.getBalanceSize(objJson,"transaction","Memos");
MrAI 12:1b24ea479a59 324 // for (int i = 0; i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 325 // {
MrAI 12:1b24ea479a59 326 // string MemoType = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "MemoType");
MrAI 12:1b24ea479a59 327 // string MemoData = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "MemoData");
MrAI 12:1b24ea479a59 328 // string parsed_memo_type = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "parsed_memo_type");
MrAI 12:1b24ea479a59 329 // cout << i << ":" << MemoType << "--" << MemoData << "--" << parsed_memo_type << endl;
MrAI 12:1b24ea479a59 330 // }
MrAI 12:1b24ea479a59 331 int date = pay.getKeyValueInt(objJson, "transaction", "date");
MrAI 12:1b24ea479a59 332 string hash = pay.getKeyValue(objJson, "transaction", "hash");
MrAI 12:1b24ea479a59 333 int inLedger = pay.getKeyValueInt(objJson, "transaction", "inLedger");
MrAI 12:1b24ea479a59 334 int ledger_index = pay.getKeyValueInt(objJson, "transaction", "ledger_index");
MrAI 12:1b24ea479a59 335 bool validated = pay.getKeyValueBool(objJson, "transaction", "validated");
MrAI 12:1b24ea479a59 336
MrAI 12:1b24ea479a59 337 }
MrAI 12:1b24ea479a59 338 return 0;
MrAI 12:1b24ea479a59 339 }
MrAI 12:1b24ea479a59 340
MrAI 12:1b24ea479a59 341 int sd_getpaymenthistory()
MrAI 12:1b24ea479a59 342 {
MrAI 12:1b24ea479a59 343 Payment pay;
MrAI 12:1b24ea479a59 344 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 345 MbedJSONValue mbJson;
MrAI 12:1b24ea479a59 346 string strJson = "";
MrAI 12:1b24ea479a59 347 string strUrl = "/v1/accounts/payments/";
MrAI 12:1b24ea479a59 348 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 349 string src_acc = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 350 string dst_acc = "69Hu8QsnEZUP4f3AVYSW7rkpLVm6DAEq9y";
MrAI 12:1b24ea479a59 351 string direction = "incoming";
MrAI 12:1b24ea479a59 352 int start_ledger = 1;
MrAI 12:1b24ea479a59 353 int end_ledger = 10000;
MrAI 12:1b24ea479a59 354 int per_page = 10;
MrAI 12:1b24ea479a59 355 int page = 1;
MrAI 12:1b24ea479a59 356 pay.SetJsonBody(mbJson, src_acc, dst_acc, direction, start_ledger, end_ledger, per_page, page);
MrAI 12:1b24ea479a59 357 strJson = pay.PostPayment(mbJson, strUrl, strWallet, "");
MrAI 12:1b24ea479a59 358 if (strJson.empty())
MrAI 12:1b24ea479a59 359 {
MrAI 12:1b24ea479a59 360 return -1;
MrAI 12:1b24ea479a59 361 }
MrAI 12:1b24ea479a59 362 string err = pay.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 363 bool bSuccess = pay.getRequestRet(objJson);
MrAI 12:1b24ea479a59 364 if (bSuccess)
MrAI 12:1b24ea479a59 365 {
MrAI 12:1b24ea479a59 366 int iCurrenciesNum = pay.getBalanceSize(objJson,"payments");
MrAI 12:1b24ea479a59 367 for (int i = 0; i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 368 {
MrAI 12:1b24ea479a59 369 string hash = pay.getBalanceArray(objJson, "payments", i, "hash");
MrAI 12:1b24ea479a59 370 string ledger = pay.getBalanceArray(objJson, "payments", i, "ledger");
MrAI 12:1b24ea479a59 371 string source_account = pay.getBalanceArray(objJson, "payments", i, "source_account");
MrAI 12:1b24ea479a59 372 string destination_account = pay.getBalanceArray(objJson, "payments", i, "destination_account");
MrAI 12:1b24ea479a59 373 string currency = pay.getBalanceArray(objJson, "payments", "amount", i,currency);
MrAI 12:1b24ea479a59 374 string value = pay.getBalanceArray(objJson, "payments", "amount", i, value);
MrAI 12:1b24ea479a59 375 string issuer = pay.getBalanceArray(objJson, "payments", "amount", i, issuer);
MrAI 12:1b24ea479a59 376 string direction = pay.getBalanceArray(objJson, "payments", i, "direction");
MrAI 12:1b24ea479a59 377 string timestamp = pay.getBalanceArray(objJson, "payments", i, "timestamp");
MrAI 12:1b24ea479a59 378 string fee = pay.getBalanceArray(objJson, "payments", i, "fee");
MrAI 12:1b24ea479a59 379 int iCurrenciesNum = pay.getBalanceSize(objJson, "transaction", "Memos");
MrAI 12:1b24ea479a59 380 for (int j = 0; j < iCurrenciesNum; j++)
MrAI 12:1b24ea479a59 381 {
MrAI 12:1b24ea479a59 382 string memo_type = pay.getBalanceArray(objJson, "transaction", "memos", i, "memo_type");
MrAI 12:1b24ea479a59 383 string memo_data = pay.getBalanceArray(objJson, "transaction", "memos", i, "memo_data");
MrAI 12:1b24ea479a59 384 cout << i << ":" << memo_type << "--" << memo_data << endl;
MrAI 12:1b24ea479a59 385 }
MrAI 12:1b24ea479a59 386 }
MrAI 12:1b24ea479a59 387 }
MrAI 12:1b24ea479a59 388 return 0;
MrAI 12:1b24ea479a59 389 }
MrAI 12:1b24ea479a59 390
MrAI 12:1b24ea479a59 391 int sd_getdefaultfee()
MrAI 12:1b24ea479a59 392 {
MrAI 12:1b24ea479a59 393 ServerInfo serInfo;
MrAI 12:1b24ea479a59 394 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 395 string strJson = "";
MrAI 12:1b24ea479a59 396 string strUrl = "/v1/transaction-fee";
MrAI 12:1b24ea479a59 397 strJson = serInfo.GetPaymentInfo(strUrl, "", "");
MrAI 12:1b24ea479a59 398 if (strJson.empty())
MrAI 12:1b24ea479a59 399 {
MrAI 12:1b24ea479a59 400 return -1;
MrAI 12:1b24ea479a59 401 }
MrAI 12:1b24ea479a59 402 string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 403 bool bSuccess = serInfo.getRequestRet(objJson);
MrAI 12:1b24ea479a59 404 if (bSuccess)
MrAI 12:1b24ea479a59 405 {
MrAI 12:1b24ea479a59 406 string fee = serInfo.getKeyValue(objJson, "fee");
MrAI 12:1b24ea479a59 407 cout << "fee:" << fee << endl;
MrAI 12:1b24ea479a59 408 }
MrAI 12:1b24ea479a59 409 return 0;
MrAI 12:1b24ea479a59 410 }
MrAI 12:1b24ea479a59 411
MrAI 12:1b24ea479a59 412 int sd_getserverinfo()
MrAI 12:1b24ea479a59 413 {
MrAI 12:1b24ea479a59 414 ServerInfo serInfo;
MrAI 12:1b24ea479a59 415 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 416 string strJson = "";
MrAI 12:1b24ea479a59 417 string strUrl = "/v1/server";
MrAI 12:1b24ea479a59 418 strJson = serInfo.GetPaymentInfo(strUrl, "", "");
MrAI 12:1b24ea479a59 419 if (strJson.empty())
MrAI 12:1b24ea479a59 420 {
MrAI 12:1b24ea479a59 421 return -1;
MrAI 12:1b24ea479a59 422 }
MrAI 12:1b24ea479a59 423 string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 424 bool bSuccess = serInfo.getRequestRet(objJson);
MrAI 12:1b24ea479a59 425 if (bSuccess)
MrAI 12:1b24ea479a59 426 {
MrAI 12:1b24ea479a59 427 string complete_ledgers = serInfo.getKeyValue(objJson, "sdchaind_server_status", "complete_ledgers");
MrAI 12:1b24ea479a59 428 string server_state = serInfo.getKeyValue(objJson, "sdchaind_server_status", "server_state");
MrAI 12:1b24ea479a59 429 int reserve_base_sda = serInfo.getKeyValueInt(objJson, "sdchaind_server_status", "reserve_base_sda");
MrAI 12:1b24ea479a59 430 cout << "complete_ledgers:" << complete_ledgers << "server_state:" << server_state << "reserve_base_sda:" << reserve_base_sda << endl;
MrAI 12:1b24ea479a59 431 }
MrAI 12:1b24ea479a59 432 return 0;
MrAI 12:1b24ea479a59 433 }
MrAI 12:1b24ea479a59 434
MrAI 12:1b24ea479a59 435 int sd_getserverconnected()
MrAI 12:1b24ea479a59 436 {
MrAI 12:1b24ea479a59 437 ServerInfo serInfo;
MrAI 12:1b24ea479a59 438 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 439 string strJson = "";
MrAI 12:1b24ea479a59 440 string strUrl = "/v1/server/connected";
MrAI 12:1b24ea479a59 441 strJson = serInfo.GetPaymentInfo(strUrl, "", "");
MrAI 12:1b24ea479a59 442 if (strJson.empty())
MrAI 12:1b24ea479a59 443 {
MrAI 12:1b24ea479a59 444 return -1;
MrAI 12:1b24ea479a59 445 }
MrAI 12:1b24ea479a59 446 string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 447 bool bSuccess = serInfo.getRequestRet(objJson);
MrAI 12:1b24ea479a59 448 if (bSuccess)
MrAI 12:1b24ea479a59 449 {
MrAI 12:1b24ea479a59 450 bool connected = serInfo.getKeyValueBool(objJson, "connected");
MrAI 12:1b24ea479a59 451 }
MrAI 12:1b24ea479a59 452 return 0;
MrAI 12:1b24ea479a59 453 }
MrAI 12:1b24ea479a59 454
MrAI 12:1b24ea479a59 455 int sd_queryorderlist()
MrAI 12:1b24ea479a59 456 {
MrAI 12:1b24ea479a59 457 Order orderInfo;
MrAI 12:1b24ea479a59 458 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 459 string strJson = "";
MrAI 12:1b24ea479a59 460 string strUrl = "/v1/accounts/orders/";
MrAI 12:1b24ea479a59 461 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 462 strJson = orderInfo.GetPaymentInfo(strUrl, strWallet, "");
MrAI 12:1b24ea479a59 463 if (strJson.empty())
MrAI 12:1b24ea479a59 464 {
MrAI 12:1b24ea479a59 465 return -1;
MrAI 12:1b24ea479a59 466 }
MrAI 12:1b24ea479a59 467 string err = orderInfo.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 468 bool bSuccess = orderInfo.getRequestRet(objJson);
MrAI 12:1b24ea479a59 469 if (bSuccess)
MrAI 12:1b24ea479a59 470 {
MrAI 12:1b24ea479a59 471 int ledger = objJson["ledger"].get<int>();
MrAI 12:1b24ea479a59 472 bool validated = orderInfo.getKeyValueBool(objJson, "validated");
MrAI 12:1b24ea479a59 473 int iCurrenciesNum = orderInfo.getBalanceSize(objJson, "orders");
MrAI 12:1b24ea479a59 474 for (int i = 0; i < iCurrenciesNum; i++)
MrAI 12:1b24ea479a59 475 {
MrAI 12:1b24ea479a59 476 string type = orderInfo.getBalanceArray(objJson, "orders", i, "type");
MrAI 12:1b24ea479a59 477 string currency_gets = orderInfo.getBalanceArray(objJson, "orders","taker_gets",i,"currency");
MrAI 12:1b24ea479a59 478 string value_gets = orderInfo.getBalanceArray(objJson, "orders", "taker_gets", i, "value");
MrAI 12:1b24ea479a59 479 string counterparty_gets = orderInfo.getBalanceArray(objJson, "orders", "taker_gets", i, "counterparty");
MrAI 12:1b24ea479a59 480 string currency_pays = orderInfo.getBalanceArray(objJson, "orders", "taker_pays", i, "currency");
MrAI 12:1b24ea479a59 481 string value_pays = orderInfo.getBalanceArray(objJson, "orders", "taker_pays", i, "value");
MrAI 12:1b24ea479a59 482 string counterparty_pays = orderInfo.getBalanceArray(objJson, "orders", "taker_pays", i, "counterparty");
MrAI 12:1b24ea479a59 483 cout << "type:" << type << "currency_gets:" << currency_gets << "value_gets:" << value_gets
MrAI 12:1b24ea479a59 484 << "counterparty_gets:" << counterparty_gets << "currency_pays:" << currency_pays
MrAI 12:1b24ea479a59 485 << "value_pays:" << value_pays << "counterparty_pays:" << counterparty_pays << endl;
MrAI 12:1b24ea479a59 486 }
MrAI 12:1b24ea479a59 487 cout << "ledger:" << ledger << "validated:" << validated << endl;
MrAI 12:1b24ea479a59 488 }
MrAI 12:1b24ea479a59 489 return 0;
MrAI 12:1b24ea479a59 490 }
MrAI 12:1b24ea479a59 491
MrAI 12:1b24ea479a59 492 int sd_commitorder()
MrAI 12:1b24ea479a59 493 {
MrAI 12:1b24ea479a59 494 Order orderCommit;
MrAI 12:1b24ea479a59 495 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 496 MbedJSONValue mbJson;
MrAI 12:1b24ea479a59 497 string strJson = "";
MrAI 12:1b24ea479a59 498 string strUrl = "/v1/accounts/orders/";
MrAI 12:1b24ea479a59 499 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 500 string secret = "sh7dqVn8mhg6BqGfAQyaYVUwK2zkR";
MrAI 12:1b24ea479a59 501 orderCommit.SetJsonBody(mbJson, secret, "buy", "SDA", "", "4", "CNY", "6UPd52jHtu1d88nc3S3WeroACFQpKfybhU", "22");
MrAI 12:1b24ea479a59 502 strJson = orderCommit.PostPayment(mbJson, strUrl, strWallet, "");
MrAI 12:1b24ea479a59 503 if (strJson.empty())
MrAI 12:1b24ea479a59 504 {
MrAI 12:1b24ea479a59 505 return -1;
MrAI 12:1b24ea479a59 506 }
MrAI 12:1b24ea479a59 507 string err = orderCommit.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 508 bool bSuccess = orderCommit.getRequestRet(objJson);
MrAI 12:1b24ea479a59 509 if (bSuccess)
MrAI 12:1b24ea479a59 510 {
MrAI 12:1b24ea479a59 511 string account = orderCommit.getKeyValue(objJson, "order", "account");
MrAI 12:1b24ea479a59 512 string currency_g = orderCommit.getKeyValue(objJson, "taker_gets", "currency");
MrAI 12:1b24ea479a59 513 string value_g = orderCommit.getKeyValue(objJson, "taker_gets", "value");
MrAI 12:1b24ea479a59 514 string counterparty_g = orderCommit.getKeyValue(objJson, "taker_gets", "counterparty");
MrAI 12:1b24ea479a59 515 string currency_p = orderCommit.getKeyValue(objJson, "taker_pays", "currency");
MrAI 12:1b24ea479a59 516 string value_p = orderCommit.getKeyValue(objJson, "taker_pays", "value");
MrAI 12:1b24ea479a59 517 string counterparty_p = orderCommit.getKeyValue(objJson, "taker_pays", "counterparty");
MrAI 12:1b24ea479a59 518 string fee = orderCommit.getKeyValue(objJson, "order", "fee");
MrAI 12:1b24ea479a59 519 string type = orderCommit.getKeyValue(objJson, "order", "type");
MrAI 12:1b24ea479a59 520 int sequence = orderCommit.getKeyValueInt(objJson, "order","sequence");
MrAI 12:1b24ea479a59 521 string hash = orderCommit.getKeyValue(objJson, "hash");
MrAI 12:1b24ea479a59 522 int ledger = orderCommit.getLedger(objJson);
MrAI 12:1b24ea479a59 523 string state = orderCommit.getKeyValue(objJson, "state");
MrAI 12:1b24ea479a59 524 }
MrAI 12:1b24ea479a59 525 return 0;
MrAI 12:1b24ea479a59 526 }
MrAI 12:1b24ea479a59 527
MrAI 12:1b24ea479a59 528 int sd_deleteorder()
MrAI 12:1b24ea479a59 529 {
MrAI 12:1b24ea479a59 530 Order orderCommit;
MrAI 12:1b24ea479a59 531 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 532 MbedJSONValue mbJson;
MrAI 12:1b24ea479a59 533 MbedJSONValue val;
MrAI 12:1b24ea479a59 534 string strJson = "";
MrAI 12:1b24ea479a59 535 string strUrl = "/v1/accounts/orders/";
MrAI 12:1b24ea479a59 536 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 537 string strNum = "1234";
MrAI 12:1b24ea479a59 538 val["secret"] = "sh7dqVn8mhg6BqGfAQyaYVUwK2zkR";
MrAI 12:1b24ea479a59 539 strJson = orderCommit.PostOrder(val, strUrl, strWallet, strNum);
MrAI 12:1b24ea479a59 540 if (strJson.empty())
MrAI 12:1b24ea479a59 541 {
MrAI 12:1b24ea479a59 542 return -1;
MrAI 12:1b24ea479a59 543 }
MrAI 12:1b24ea479a59 544 string err = orderCommit.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 545 bool bSuccess = orderCommit.getRequestRet(objJson);
MrAI 12:1b24ea479a59 546 if (bSuccess)
MrAI 12:1b24ea479a59 547 {
MrAI 12:1b24ea479a59 548 string account = orderCommit.getKeyValue(objJson, "order", "account");
MrAI 12:1b24ea479a59 549 string fee = orderCommit.getKeyValue(objJson, "order", "fee");
MrAI 12:1b24ea479a59 550 string offer_sequence = orderCommit.getKeyValue(objJson, "order", "offer_sequence");
MrAI 12:1b24ea479a59 551 string sequence = orderCommit.getKeyValue(objJson, "order", "sequence");
MrAI 12:1b24ea479a59 552 string hash = orderCommit.getKeyValue(objJson, "hash");
MrAI 12:1b24ea479a59 553 string ledger = orderCommit.getKeyValue(objJson, "ledger");
MrAI 12:1b24ea479a59 554 string state = orderCommit.getKeyValue(objJson, "state");
MrAI 12:1b24ea479a59 555 cout << "account:" << account << "fee:" << fee << "offer_sequence:" << offer_sequence
MrAI 12:1b24ea479a59 556 << "sequence:" << sequence << "hash:" << hash << "ledger:" << ledger << "state:" << state << endl;
MrAI 12:1b24ea479a59 557 }
MrAI 12:1b24ea479a59 558 return 0;
MrAI 12:1b24ea479a59 559 }
MrAI 12:1b24ea479a59 560
MrAI 12:1b24ea479a59 561 int sd_queryorderdetailed()
MrAI 12:1b24ea479a59 562 {
MrAI 12:1b24ea479a59 563 cout << "call sd_queryorderdetailed begin" << endl;
MrAI 12:1b24ea479a59 564 Order orderInfo;
MrAI 12:1b24ea479a59 565 MbedJSONValue objJson;
MrAI 12:1b24ea479a59 566 string strJson = "";
MrAI 12:1b24ea479a59 567 string strUrl = " /v1/accounts/orders/";
MrAI 12:1b24ea479a59 568 string strWallet = "6ByxwCh2Lqwwr3pk6mh4GBb4bsqFz2zAKY";
MrAI 12:1b24ea479a59 569 string hash = "EF252979355F2666486F69E9C96D137265214DE65B14E1136ECCDCE84F09AD11";
MrAI 12:1b24ea479a59 570 strJson = orderInfo.GetPaymentInfo(strUrl, strWallet,hash);
MrAI 12:1b24ea479a59 571 if (strJson.empty())
MrAI 12:1b24ea479a59 572 {
MrAI 12:1b24ea479a59 573 cout << "call sd_queryorderdetailed exception quit" << endl;
MrAI 12:1b24ea479a59 574 return -1;
MrAI 12:1b24ea479a59 575 }
MrAI 12:1b24ea479a59 576 string err = orderInfo.getParseMbedJson(objJson, strJson.c_str());
MrAI 12:1b24ea479a59 577 bool bSuccess = orderInfo.getRequestRet(objJson);
MrAI 12:1b24ea479a59 578 if (bSuccess)
MrAI 12:1b24ea479a59 579 {
MrAI 12:1b24ea479a59 580 string hash = orderInfo.getKeyValue(objJson, "hash");
MrAI 12:1b24ea479a59 581 int ledger = objJson["ledger"].get<int>();
MrAI 12:1b24ea479a59 582 int iBalanceNum = orderInfo.getBalanceSize(objJson, "balance_changes");
MrAI 12:1b24ea479a59 583 for (int i = 0; i < iBalanceNum; i++)
MrAI 12:1b24ea479a59 584 {
MrAI 12:1b24ea479a59 585 string counterparty = orderInfo.getBalanceArray(objJson, "balance_changes", i, "counterparty");
MrAI 12:1b24ea479a59 586 }
MrAI 12:1b24ea479a59 587 int iOrderChangesNum = orderInfo.getBalanceSize(objJson, "order_changes");
MrAI 12:1b24ea479a59 588 for (int j = 0; j < iOrderChangesNum; j++)
MrAI 12:1b24ea479a59 589 {
MrAI 12:1b24ea479a59 590 string currency_gets = orderInfo.getBalanceArray(objJson, "order_changes", "taker_gets", j, "currency");
MrAI 12:1b24ea479a59 591 }
MrAI 12:1b24ea479a59 592
MrAI 12:1b24ea479a59 593 }
MrAI 12:1b24ea479a59 594 cout << "call sd_queryorderdetailed end" << endl;
MrAI 12:1b24ea479a59 595 return 0;
MrAI 12:1b24ea479a59 596 }
webmaster 8:f2a567ee3a46 597 int main(void)
webmaster 8:f2a567ee3a46 598 {
webmaster 8:f2a567ee3a46 599 printf("main start\n");
webmaster 8:f2a567ee3a46 600
MrAI 12:1b24ea479a59 601 // test_uuid();
webmaster 10:aabd720e632c 602
webmaster 9:d7468574ef8f 603
webmaster 8:f2a567ee3a46 604 socket_init();
webmaster 8:f2a567ee3a46 605
MrAI 12:1b24ea479a59 606 //test_MockHsm();
MrAI 12:1b24ea479a59 607 // test_Asset();
MrAI 12:1b24ea479a59 608 // test_Account();
MrAI 12:1b24ea479a59 609
MrAI 12:1b24ea479a59 610 sd_wallet();
MrAI 12:1b24ea479a59 611 sd_account();
MrAI 12:1b24ea479a59 612 //sd_postpayment();
MrAI 12:1b24ea479a59 613 //sd_getpaymentinfo();
MrAI 12:1b24ea479a59 614 //sd_gettransactions();
MrAI 12:1b24ea479a59 615 //sd_getpaymenthistory();
MrAI 12:1b24ea479a59 616 //sd_getdefaultfee();
MrAI 12:1b24ea479a59 617 //sd_getserverinfo();
MrAI 12:1b24ea479a59 618 //sd_getserverconnected();
MrAI 12:1b24ea479a59 619 //sd_queryorderlist();
MrAI 12:1b24ea479a59 620 //sd_commitorder();
MrAI 12:1b24ea479a59 621 //sd_deleteorder();
MrAI 12:1b24ea479a59 622 //sd_queryorderdetailed();
MrAI 12:1b24ea479a59 623
MrAI 12:1b24ea479a59 624
webmaster 10:aabd720e632c 625
webmaster 10:aabd720e632c 626 //char isContinue = 'y';
webmaster 10:aabd720e632c 627 // do
webmaster 10:aabd720e632c 628 //{
webmaster 10:aabd720e632c 629 string type = "spend_account";
webmaster 10:aabd720e632c 630 //string type = "issue";
webmaster 10:aabd720e632c 631 //test_transaction(type);
webmaster 10:aabd720e632c 632 test_transaction();
webmaster 10:aabd720e632c 633 printf("=============5========\n");
webmaster 10:aabd720e632c 634 // }while(1);
webmaster 10:aabd720e632c 635 //}while (isContinue = getch() != 'q');
webmaster 8:f2a567ee3a46 636
webmaster 10:aabd720e632c 637
webmaster 10:aabd720e632c 638 //test_socket_client();
webmaster 8:f2a567ee3a46 639
webmaster 8:f2a567ee3a46 640 socket_fini();
webmaster 8:f2a567ee3a46 641
webmaster 8:f2a567ee3a46 642 printf("main end\n");
webmaster 8:f2a567ee3a46 643 return 0;
webmaster 8:f2a567ee3a46 644 }
webmaster 8:f2a567ee3a46 645
webmaster 8:f2a567ee3a46 646 int socket_init()
webmaster 8:f2a567ee3a46 647 {
webmaster 8:f2a567ee3a46 648 int ret = 0;
webmaster 8:f2a567ee3a46 649 // EthernetInterface eth;
webmaster 8:f2a567ee3a46 650 printf("Initializing Ethernet\n");
webmaster 8:f2a567ee3a46 651 ret = eth.init(); //Use DHCP
webmaster 8:f2a567ee3a46 652 //eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use static ip
webmaster 8:f2a567ee3a46 653 printf("Connecting\n");
webmaster 8:f2a567ee3a46 654 ret = eth.connect();
webmaster 8:f2a567ee3a46 655 printf("IP Address is %s\n", eth.getIPAddress());
webmaster 8:f2a567ee3a46 656
webmaster 8:f2a567ee3a46 657 return ret;
webmaster 8:f2a567ee3a46 658 }
webmaster 8:f2a567ee3a46 659
webmaster 8:f2a567ee3a46 660 int socket_fini()
webmaster 8:f2a567ee3a46 661 {
webmaster 8:f2a567ee3a46 662 return eth.disconnect();
webmaster 8:f2a567ee3a46 663 }
webmaster 8:f2a567ee3a46 664
webmaster 8:f2a567ee3a46 665 int test_socket_client(void)
webmaster 8:f2a567ee3a46 666 {
webmaster 8:f2a567ee3a46 667 TCPSocketConnection socket;
webmaster 8:f2a567ee3a46 668 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
webmaster 8:f2a567ee3a46 669 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
webmaster 8:f2a567ee3a46 670 wait(1);
webmaster 8:f2a567ee3a46 671 }
webmaster 8:f2a567ee3a46 672 printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
webmaster 8:f2a567ee3a46 673
webmaster 8:f2a567ee3a46 674 // Send message to server
webmaster 8:f2a567ee3a46 675 char hello[] = "Hello World";
webmaster 8:f2a567ee3a46 676 printf("Sending message to Server : '%s' \n",hello);
webmaster 8:f2a567ee3a46 677 socket.send_all(hello, sizeof(hello) - 1);
webmaster 8:f2a567ee3a46 678
webmaster 8:f2a567ee3a46 679 // Receive message from server
webmaster 8:f2a567ee3a46 680 char buf[256];
webmaster 8:f2a567ee3a46 681 int n = socket.receive(buf, 256);
webmaster 8:f2a567ee3a46 682 buf[n] = '\0';
webmaster 8:f2a567ee3a46 683 printf("Received message from server: '%s'\n", buf);
webmaster 8:f2a567ee3a46 684
webmaster 8:f2a567ee3a46 685 // Clean up
webmaster 8:f2a567ee3a46 686 socket.close();
webmaster 8:f2a567ee3a46 687
webmaster 8:f2a567ee3a46 688 return 0;
webmaster 8:f2a567ee3a46 689 }
webmaster 8:f2a567ee3a46 690
MrAI 11:6d9cefb14158 691 /*
gsteiert 0:28bc7ce77e20 692 void get_file(char* uri)
gsteiert 0:28bc7ce77e20 693 {
gsteiert 3:4f71a37a1ed2 694 uart.printf("get_file %s\n", uri);
gsteiert 0:28bc7ce77e20 695 char *lstchr = strrchr(uri, NULL) -1;
gsteiert 0:28bc7ce77e20 696 if ('/' == *lstchr) {
gsteiert 3:4f71a37a1ed2 697 uart.printf("Open directory /sd%s\n", uri);
gsteiert 0:28bc7ce77e20 698 *lstchr = 0;
gsteiert 3:4f71a37a1ed2 699 sprintf(fileName, "/sd%s", uri);
gsteiert 3:4f71a37a1ed2 700 DIR *d = opendir(fileName);
gsteiert 0:28bc7ce77e20 701 if (d != NULL) {
gsteiert 0:28bc7ce77e20 702 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 703 client.send(httpHeader,strlen(httpHeader));
gsteiert 3:4f71a37a1ed2 704 sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
gsteiert 0:28bc7ce77e20 705 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 706 struct dirent *p;
gsteiert 0:28bc7ce77e20 707 while((p = readdir(d)) != NULL) {
gsteiert 3:4f71a37a1ed2 708 sprintf(dirName, "%s/%s", fileName, p->d_name);
gsteiert 3:4f71a37a1ed2 709 uart.printf("%s\n", dirName);
gsteiert 3:4f71a37a1ed2 710 DIR *subDir = opendir(dirName);
gsteiert 3:4f71a37a1ed2 711 if (subDir != NULL) {
gsteiert 3:4f71a37a1ed2 712 sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
gsteiert 3:4f71a37a1ed2 713 } else {
gsteiert 3:4f71a37a1ed2 714 sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
gsteiert 3:4f71a37a1ed2 715 }
gsteiert 0:28bc7ce77e20 716 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 717 }
gsteiert 0:28bc7ce77e20 718 }
gsteiert 0:28bc7ce77e20 719 closedir(d);
gsteiert 3:4f71a37a1ed2 720 uart.printf("Directory closed\n");
gsteiert 0:28bc7ce77e20 721 sprintf(httpHeader,"</ul></body></html>");
gsteiert 0:28bc7ce77e20 722 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 723 } else {
gsteiert 3:4f71a37a1ed2 724 sprintf(fileName, "/sd%s", uri);
gsteiert 3:4f71a37a1ed2 725 fp = fopen(fileName, "r");
gsteiert 0:28bc7ce77e20 726 if (fp == NULL) {
gsteiert 7:04744a9ce2b9 727 uart.printf("File not found\n");
gsteiert 0:28bc7ce77e20 728 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 729 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 730 client.send(uri,strlen(uri));
gsteiert 0:28bc7ce77e20 731 } else {
gsteiert 7:04744a9ce2b9 732 uart.printf("Sending: header");
gsteiert 0:28bc7ce77e20 733 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 734 client.send(httpHeader,strlen(httpHeader));
gsteiert 7:04744a9ce2b9 735 uart.printf(" file");
gsteiert 0:28bc7ce77e20 736 while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
gsteiert 0:28bc7ce77e20 737 client.send(buffer, rdCnt);
gsteiert 7:04744a9ce2b9 738 uart.printf(".");
gsteiert 0:28bc7ce77e20 739 }
gsteiert 0:28bc7ce77e20 740 client.send(buffer, rdCnt);
gsteiert 0:28bc7ce77e20 741 fclose(fp);
gsteiert 7:04744a9ce2b9 742 uart.printf("done\n");
gsteiert 0:28bc7ce77e20 743 }
gsteiert 0:28bc7ce77e20 744 }
gsteiert 0:28bc7ce77e20 745 }
gsteiert 0:28bc7ce77e20 746
webmaster 8:f2a567ee3a46 747 int main_1 (void)
gsteiert 0:28bc7ce77e20 748 {
gsteiert 3:4f71a37a1ed2 749 // Serial Interface eth;
gsteiert 3:4f71a37a1ed2 750 uart.baud(115200);
gsteiert 5:27b09a0f3fb6 751 uart.printf("Initializing\n");
gsteiert 3:4f71a37a1ed2 752
gsteiert 3:4f71a37a1ed2 753 // Check File System
gsteiert 5:27b09a0f3fb6 754 uart.printf("Checking File System\n");
gsteiert 3:4f71a37a1ed2 755 DIR *d = opendir("/sd/");
gsteiert 3:4f71a37a1ed2 756 if (d != NULL) {
gsteiert 3:4f71a37a1ed2 757 uart.printf("SD Card Present\n");
gsteiert 3:4f71a37a1ed2 758 } else {
gsteiert 3:4f71a37a1ed2 759 uart.printf("SD Card Root Directory Not Found\n");
gsteiert 3:4f71a37a1ed2 760 }
gsteiert 3:4f71a37a1ed2 761
gsteiert 0:28bc7ce77e20 762 // EthernetInterface eth;
gsteiert 5:27b09a0f3fb6 763 uart.printf("Initializing Ethernet\n");
webmaster 8:f2a567ee3a46 764 //eth.init(); //Use DHCP
webmaster 8:f2a567ee3a46 765 eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use
gsteiert 5:27b09a0f3fb6 766 uart.printf("Connecting\n");
gsteiert 0:28bc7ce77e20 767 eth.connect();
gsteiert 3:4f71a37a1ed2 768 uart.printf("IP Address is %s\n", eth.getIPAddress());
webmaster 8:f2a567ee3a46 769
webmaster 8:f2a567ee3a46 770 //test socket client
webmaster 8:f2a567ee3a46 771 //my_client.connect("192.168.31.185",10003);
webmaster 8:f2a567ee3a46 772 //my_client.send_all("hello world",11);
webmaster 8:f2a567ee3a46 773
webmaster 8:f2a567ee3a46 774 TCPSocketConnection socket;
webmaster 8:f2a567ee3a46 775 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
webmaster 8:f2a567ee3a46 776 uart.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
webmaster 8:f2a567ee3a46 777 wait(1);
webmaster 8:f2a567ee3a46 778 }
webmaster 8:f2a567ee3a46 779 uart.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
webmaster 8:f2a567ee3a46 780
webmaster 8:f2a567ee3a46 781 // Send message to server
webmaster 8:f2a567ee3a46 782 char hello[] = "Hello World";
webmaster 8:f2a567ee3a46 783 uart.printf("Sending message to Server : '%s' \n",hello);
webmaster 8:f2a567ee3a46 784 socket.send_all(hello, sizeof(hello) - 1);
webmaster 8:f2a567ee3a46 785
webmaster 8:f2a567ee3a46 786 // Receive message from server
webmaster 8:f2a567ee3a46 787 char buf[256];
webmaster 8:f2a567ee3a46 788 int n = socket.receive(buf, 256);
webmaster 8:f2a567ee3a46 789 buf[n] = '\0';
webmaster 8:f2a567ee3a46 790 uart.printf("Received message from server: '%s'\n", buf);
webmaster 8:f2a567ee3a46 791
webmaster 8:f2a567ee3a46 792 // Clean up
webmaster 8:f2a567ee3a46 793 socket.close();
webmaster 8:f2a567ee3a46 794 ////////////////////////////////////////////////////////////////////////////
gsteiert 0:28bc7ce77e20 795
gsteiert 0:28bc7ce77e20 796 // TCPSocketServer server;
gsteiert 0:28bc7ce77e20 797 server.bind(HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 798 server.listen();
gsteiert 5:27b09a0f3fb6 799 uart.printf("Server Listening\n");
gsteiert 0:28bc7ce77e20 800
gsteiert 0:28bc7ce77e20 801 while (true) {
gsteiert 3:4f71a37a1ed2 802 uart.printf("\nWait for new connection...\r\n");
gsteiert 0:28bc7ce77e20 803 server.accept(client);
gsteiert 0:28bc7ce77e20 804 client.set_blocking(false, 1500); // Timeout after (1.5)s
webmaster 8:f2a567ee3a46 805
webmaster 8:f2a567ee3a46 806 my_client.send_all("hello world",11);
gsteiert 0:28bc7ce77e20 807
gsteiert 3:4f71a37a1ed2 808 uart.printf("Connection from: %s\r\n", client.get_address());
gsteiert 0:28bc7ce77e20 809 while (true) {
gsteiert 0:28bc7ce77e20 810 int n = client.receive(buffer, sizeof(buffer));
gsteiert 0:28bc7ce77e20 811 if (n <= 0) break;
gsteiert 3:4f71a37a1ed2 812 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
gsteiert 0:28bc7ce77e20 813 if (n >= 1024) {
gsteiert 0:28bc7ce77e20 814 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 815 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 816 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 817 break;
gsteiert 0:28bc7ce77e20 818 } else {
gsteiert 0:28bc7ce77e20 819 buffer[n]=0;
gsteiert 0:28bc7ce77e20 820 }
gsteiert 0:28bc7ce77e20 821 if (!strncmp(buffer, "GET ", 4)) {
gsteiert 0:28bc7ce77e20 822 uristr = buffer + 4;
gsteiert 0:28bc7ce77e20 823 eou = strstr(uristr, " ");
gsteiert 0:28bc7ce77e20 824 if (eou == NULL) {
gsteiert 0:28bc7ce77e20 825 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 826 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 827 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 828 } else {
gsteiert 0:28bc7ce77e20 829 *eou = 0;
gsteiert 3:4f71a37a1ed2 830 get_file(uristr);
gsteiert 0:28bc7ce77e20 831 }
gsteiert 0:28bc7ce77e20 832 }
gsteiert 0:28bc7ce77e20 833 }
gsteiert 0:28bc7ce77e20 834
gsteiert 0:28bc7ce77e20 835 client.close();
gsteiert 0:28bc7ce77e20 836 }
gsteiert 0:28bc7ce77e20 837 }
MrAI 11:6d9cefb14158 838 */
webmaster 8:f2a567ee3a46 839
webmaster 8:f2a567ee3a46 840 int main_2() {
webmaster 8:f2a567ee3a46 841 // Serial Interface eth;
webmaster 8:f2a567ee3a46 842 uart.baud(115200);
webmaster 8:f2a567ee3a46 843 uart.printf("Initializing\n");
webmaster 8:f2a567ee3a46 844
webmaster 8:f2a567ee3a46 845 EthernetInterface eth;
webmaster 8:f2a567ee3a46 846 eth.init(); //Use DHCP
webmaster 8:f2a567ee3a46 847 eth.connect();
webmaster 8:f2a567ee3a46 848 printf("\nClient IP Address is %s\n", eth.getIPAddress());
webmaster 8:f2a567ee3a46 849
webmaster 8:f2a567ee3a46 850 // Connect to Server
webmaster 8:f2a567ee3a46 851 TCPSocketConnection socket;
webmaster 8:f2a567ee3a46 852 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
webmaster 8:f2a567ee3a46 853 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
webmaster 8:f2a567ee3a46 854 wait(1);
webmaster 8:f2a567ee3a46 855 }
webmaster 8:f2a567ee3a46 856 printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
webmaster 8:f2a567ee3a46 857
webmaster 8:f2a567ee3a46 858 // Send message to server
webmaster 8:f2a567ee3a46 859 char hello[] = "Hello World";
webmaster 8:f2a567ee3a46 860 printf("Sending message to Server : '%s' \n",hello);
webmaster 8:f2a567ee3a46 861 socket.send_all(hello, sizeof(hello) - 1);
webmaster 8:f2a567ee3a46 862
webmaster 8:f2a567ee3a46 863 // Receive message from server
webmaster 8:f2a567ee3a46 864 char buf[256];
webmaster 8:f2a567ee3a46 865 int n = socket.receive(buf, 256);
webmaster 8:f2a567ee3a46 866 buf[n] = '\0';
webmaster 8:f2a567ee3a46 867 printf("Received message from server: '%s'\n", buf);
webmaster 8:f2a567ee3a46 868
webmaster 8:f2a567ee3a46 869 // Clean up
webmaster 8:f2a567ee3a46 870 socket.close();
webmaster 8:f2a567ee3a46 871 eth.disconnect();
webmaster 8:f2a567ee3a46 872
webmaster 8:f2a567ee3a46 873 while(true) {}
webmaster 8:f2a567ee3a46 874 }