@@ -231,17 +231,6 @@ - (instancetype)init {
231
231
return self;
232
232
}
233
233
234
- /*
235
- - (XP2PErrCode)startAppWith:(NSString *)sec_id sec_key:(NSString *)sec_key pro_id:(NSString *)pro_id dev_name:(NSString *)dev_name {
236
- return [self startAppWith:sec_id sec_key:sec_key pro_id:pro_id dev_name:dev_name xp2pinfo:@""];
237
- }
238
- - (XP2PErrCode)startAppWith:(NSString *)sec_id sec_key:(NSString *)sec_key pro_id:(NSString *)pro_id dev_name:(NSString *)dev_name xp2pinfo:(NSString *)xp2pinfo {
239
- setQcloudApiCred([sec_id UTF8String], [sec_key UTF8String]); //正式版app发布时候需要去掉,避免泄露secretid和secretkey,此处仅为演示
240
- int ret = [self startAppWith:pro_id dev_name:dev_name type:XP2P_PROTOCOL_AUTO];
241
- setDeviceXp2pInfo(dev_name.UTF8String, xp2pinfo.UTF8String);
242
- return (XP2PErrCode)ret;
243
- }
244
- */
245
234
- (const char *)dicConvertString : (NSDictionary *)dic {
246
235
NSError *error;
247
236
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dic options: kNilOptions error: &error];
@@ -258,6 +247,7 @@ - (XP2PErrCode)startAppWith:(NSString *)pro_id dev_name:(NSString *)dev_name {
258
247
config.appsecret = @" appsecret" ; // 为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
259
248
config.userid = [self getAppUUID ];
260
249
250
+ config.autoConfigFromDevice = NO ;
261
251
config.type = XP2P_PROTOCOL_AUTO;
262
252
config.crossStunTurn = NO ;
263
253
return [self startAppWith: pro_id dev_name: dev_name appconfig: config];
@@ -293,14 +283,30 @@ - (XP2PErrCode)startAppWith:(NSString *)pro_id dev_name:(NSString *)dev_name app
293
283
294
284
// 配置是否启用双中转
295
285
setCrossStunTurn (false );
296
- if (appconfig.crossStunTurn ) {
297
- setCrossStunTurn (true );
298
- }
299
286
300
287
// 1.配置IOT_P2P SDK
301
288
self.pro_id = pro_id;
302
289
self.dev_name = dev_name;
303
- int ret = startService (dev_name.UTF8String , pro_id.UTF8String , dev_name.UTF8String , appconfig.type );
290
+
291
+ int ret = XP2P_ERR_NONE;
292
+
293
+ if (appconfig.autoConfigFromDevice ) {
294
+ [self appGeDeviceConfig: appconfig]; // get config
295
+
296
+ }else {
297
+ app_config_t config_ = {
298
+ .server = " " ,
299
+ .ip = " " ,
300
+ .port = 20002 ,
301
+ .type = appconfig.type
302
+ };
303
+
304
+ if (appconfig.crossStunTurn ) {
305
+ setCrossStunTurn (true );
306
+ }
307
+ ret = startService (dev_name.UTF8String , pro_id.UTF8String , dev_name.UTF8String , config_);
308
+ }
309
+
304
310
return (XP2PErrCode)ret;
305
311
}
306
312
@@ -398,6 +404,75 @@ - (void)appGetUserConfig:(TIoTP2PAPPConfig *)appconfig {
398
404
[tasklog resume ];
399
405
}
400
406
407
+ - (void )appGeDeviceConfig : (TIoTP2PAPPConfig *)appconfig {
408
+ NSMutableDictionary *accessParam = [NSMutableDictionary dictionary ];
409
+ [accessParam setValue: @" AppDescribeConfigureDeviceP2P" forKey: @" Action" ];
410
+ [accessParam setValue: @([[TIoTCoreXP2PBridge getNowTimeTimestampSec ] integerValue ]) forKey: @" Timestamp" ];
411
+ [accessParam setValue: @(arc4random ()) forKey: @" Nonce" ];
412
+ [accessParam setValue: appconfig.appkey forKey: @" AppKey" ];
413
+ [accessParam setValue: self .pro_id forKey: @" ProductId" ];
414
+ [accessParam setValue: self .dev_name forKey: @" DeviceName" ];
415
+ [accessParam setValue: [[NSUUID UUID ] UUIDString ] forKey: @" RequestId" ];
416
+
417
+ NSString *content = createSortedQueryString (accessParam);
418
+ NSString *signature = [self signMessage: content withSecret: appconfig.appsecret];
419
+ [accessParam setValue: signature forKey: @" Signature" ];
420
+
421
+
422
+ NSURL *url = [NSURL URLWithString: @" https://iot.cloud.tencent.com/api/exploreropen/appapi" ];
423
+ NSMutableURLRequest *reqlog = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 5 ];
424
+ [reqlog setValue: @" application/json" forHTTPHeaderField: @" Content-Type" ];
425
+ reqlog.HTTPMethod = @" POST" ;
426
+ reqlog.HTTPBody = [NSJSONSerialization dataWithJSONObject: accessParam options: NSJSONWritingFragmentsAllowed error: nil ];;
427
+ NSURLSessionDataTask *tasklog = [[NSURLSession sharedSession ] dataTaskWithRequest: reqlog completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
428
+
429
+ app_config_t config_ = {
430
+ .server = " " ,
431
+ .ip = " " ,
432
+ .port = 20002 ,
433
+ .type = appconfig.type ,
434
+ .cross = false
435
+ };
436
+
437
+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
438
+ if (httpResponse.statusCode == 200 ) {
439
+ NSError *jsonerror = nil ;
440
+ NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingAllowFragments error: &jsonerror];
441
+ // NSLog(@"log serverapi:content===>%@, param==>%@, data===>%@",content,accessParam,dic);
442
+ NSDictionary *deviceconfig = [[dic objectForKey: @" data" ] objectForKey: @" Config" ];
443
+
444
+ bool enableCrossStunTurn = [[deviceconfig objectForKey: @" EnableCrossStunTurn" ] boolValue ];
445
+ int stunPort = [[deviceconfig objectForKey: @" StunPort" ] intValue ];
446
+ NSString *stunHost = [deviceconfig objectForKey: @" StunHost" ];
447
+ NSString *stunIP = [deviceconfig objectForKey: @" StunIP" ];
448
+ NSString *protocol = [deviceconfig objectForKey: @" Protocol" ];
449
+
450
+ config_.cross = enableCrossStunTurn;
451
+ if (stunPort) {
452
+ config_.port = stunPort;
453
+ }
454
+ if (stunHost.length > 0 ) {
455
+ config_.server = stunHost.UTF8String ;
456
+ }
457
+ if (stunIP.length > 0 ) {
458
+ config_.ip = stunIP.UTF8String ;
459
+ }
460
+ if ([protocol isEqualToString: @" TCP" ]) {
461
+ config_.type = XP2P_PROTOCOL_TCP;
462
+ }else {
463
+ config_.type = XP2P_PROTOCOL_AUTO;
464
+ }
465
+ }
466
+
467
+
468
+ if (config_.cross ) {
469
+ setCrossStunTurn (true );
470
+ }
471
+ startService (self.dev_name .UTF8String , self.pro_id .UTF8String , self.dev_name .UTF8String , config_);
472
+ }];
473
+ [tasklog resume ];
474
+ }
475
+
401
476
- (NSString *)getUrlForHttpFlv : (NSString *)dev_name {
402
477
const char *httpflv = delegateHttpFlv (dev_name.UTF8String );
403
478
NSLog (@" httpflv---%s " ,httpflv);
@@ -845,9 +920,6 @@ - (void)setAppConfig:(NSDictionary *)appconfig {
845
920
846
921
[self saveKeychainValue: tmp_p2p_log_enabled key: @" p2p_log_enabled" ];
847
922
[self saveKeychainValue: tmp_ops_report_enabled key: @" ops_report_enabled" ];
848
-
849
- p2p_log_enabled = tmp_p2p_log_enabled.boolValue ;
850
- ops_report_enabled = tmp_ops_report_enabled.boolValue ;
851
923
}
852
924
853
925
+ (NSString *)getSDKVersion {
0 commit comments