SoftbusSubscribe.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <jni.h>
  5. #include "securec.h"
  6. #include "discovery_service.h"
  7. #include "softbus_bus_center.h"
  8. #include "com_jg_softbus_naservice_SubscribeService.h"
  9. // 全局变量,用于保存Java虚拟机的引用
  10. JavaVM *jvm;
  11. //全局引用变量,用于保存java对象
  12. jobject globalRef;
  13. /**
  14. * 解析java对象为c的SubscribeInfo结构
  15. */
  16. int ParseSubscribeInfo(JNIEnv *env, jobject subscribeInfo, SubscribeInfo *info) {
  17. jclass infoClass = (*env)->GetObjectClass(env, subscribeInfo);
  18. //读取subscribeId
  19. jmethodID getSubscribeIdMethod = (*env)->GetMethodID(env, infoClass, "getSubscribeId", "()I");
  20. jint subscribeId = (*env)->CallIntMethod(env, subscribeInfo, getSubscribeIdMethod);
  21. info->subscribeId = subscribeId;
  22. printf("SubscribeInfo->subscribeId:%d\n", subscribeId);
  23. //获取mode
  24. jmethodID getDiscoverModeMethod = (*env)->GetMethodID(env, infoClass, "getMode", "()Lcom/jg/softbus/discovery/enums/DiscoverMode;");
  25. jobject modeInfo = (*env)->CallObjectMethod(env, subscribeInfo, getDiscoverModeMethod);
  26. jclass modeClass = (*env)->GetObjectClass(env, modeInfo);
  27. jmethodID getModeMethod = (*env)->GetMethodID(env, modeClass, "valueOf", "()I");
  28. jint mode = (*env)->CallIntMethod(env, modeInfo, getModeMethod);
  29. info->mode = mode;
  30. (*env)->DeleteLocalRef(env, modeInfo);
  31. printf("SubscribeInfo->mode:%d\n", mode);
  32. //获取medium
  33. jmethodID getMediumMethod = (*env)->GetMethodID(env, infoClass, "getMedium", "()Lcom/jg/softbus/discovery/enums/ExchangeMedium;");
  34. jobject mediumInfo = (*env)->CallObjectMethod(env, subscribeInfo, getMediumMethod);
  35. jclass mediumClass = (*env)->GetObjectClass(env, mediumInfo);
  36. jmethodID getMediumMethod2 = (*env)->GetMethodID(env, mediumClass, "valueOf", "()I");
  37. jint medium = (*env)->CallIntMethod(env, mediumInfo, getMediumMethod2);
  38. info->medium = medium;
  39. (*env)->DeleteLocalRef(env, mediumInfo);
  40. printf("SubscribeInfo->medium:%d\n", medium);
  41. //获取freq频次
  42. jmethodID getFreqMethod = (*env)->GetMethodID(env, infoClass, "getFreq", "()Lcom/jg/softbus/discovery/enums/ExchangeFreq;");
  43. jobject freqInfo = (*env)->CallObjectMethod(env, subscribeInfo, getFreqMethod);
  44. jclass freqClass = (*env)->GetObjectClass(env, freqInfo);
  45. jmethodID getFreqMethod2 = (*env)->GetMethodID(env, freqClass, "valueOf", "()I");
  46. jint freq = (*env)->CallIntMethod(env, freqInfo, getFreqMethod2);
  47. info->freq = freq;
  48. (*env)->DeleteLocalRef(env, freqInfo);
  49. printf("SubscribeInfo->freq:%d\n", freq);
  50. //获取isSameAccount bool类型
  51. jmethodID getIsSameAccountMethod = (*env)->GetMethodID(env, infoClass, "getIsSameAccount", "()Z");
  52. info->isSameAccount = (*env)->CallBooleanMethod(env, subscribeInfo, getIsSameAccountMethod);
  53. printf("SubscribeInfo->isSameAccount:%d\n", info->isSameAccount);
  54. //获取isWakeRemote bool类型
  55. jmethodID getIsWakeRemoteMethod = (*env)->GetMethodID(env, infoClass, "getIsWakeRemote", "()Z");
  56. info->isWakeRemote = (*env)->CallBooleanMethod(env, subscribeInfo, getIsWakeRemoteMethod);
  57. printf("SubscribeInfo->isWakeRemote:%d\n", info->isWakeRemote);
  58. //获取capability
  59. jmethodID getCapabilityMethod = (*env)->GetMethodID(env, infoClass, "getCapability", "()Ljava/lang/String;");
  60. jstring capability = (*env)->CallObjectMethod(env, subscribeInfo, getCapabilityMethod);
  61. const char *capabilityStr = (*env)->GetStringUTFChars(env, capability, 0);
  62. info->capability = capabilityStr;
  63. if(info->capability == NULL) {
  64. info->capability = "osdCapability";
  65. }
  66. (*env)->ReleaseStringUTFChars(env, capability, capabilityStr);
  67. printf("SubscribeInfo->capability:%s\n", capabilityStr);
  68. //获取capabilityData
  69. jmethodID getCapabilityDataMethod = (*env)->GetMethodID(env, infoClass, "getCapabilityData", "()Ljava/lang/String;");
  70. jstring capabilityData = (*env)->CallObjectMethod(env, subscribeInfo, getCapabilityDataMethod);
  71. const char *capabilityDataStr = (*env)->GetStringUTFChars(env, capabilityData, 0);
  72. strcpy(info->capabilityData, capabilityDataStr);
  73. if(info->capabilityData == NULL) {
  74. info->capabilityData = NULL;
  75. }
  76. (*env)->ReleaseStringUTFChars(env, capabilityData, capabilityDataStr);
  77. printf("SubscribeInfo->capabilityData:%s\n", capabilityDataStr);
  78. //获取数据长度
  79. jmethodID getDataLenMethod = (*env)->GetMethodID(env, infoClass, "getDataLen", "()I");
  80. info->dataLen = (*env)->CallIntMethod(env, subscribeInfo, getDataLenMethod);
  81. printf("SubscribeInfo->dataLen:%d\n", info->dataLen);
  82. }
  83. /**
  84. * 将c的DeviceInfo对象转换为java的object对象
  85. */
  86. jobject DeviceInfoToJavaObject(JNIEnv *env, const DeviceInfo *device) {
  87. //创建java的DeviceInfo对象
  88. jclass deviceInfoClass = (*env)->FindClass(env, "com/jg/softbus/device/DeviceInfo");
  89. if (deviceInfoClass == NULL) {
  90. // 处理错误
  91. printf("Cannot found the java class:com/jg/softbus/device/DeviceInfo");
  92. return NULL;
  93. }
  94. jmethodID constructor = (*env)->GetMethodID(env, deviceInfoClass, "<init>", "()V");
  95. if (constructor == NULL) {
  96. // 处理错误
  97. printf("Cannot found the default constructor of DeviceInfo.");
  98. return NULL;
  99. }
  100. jobject obj = (*env)->NewObject(env, deviceInfoClass, constructor);
  101. if (obj == NULL) {
  102. // 处理错误
  103. printf("Cannot create the DeviceInfo object.");
  104. return NULL;
  105. }
  106. //设置devId
  107. jmethodID setDevIdMethod = (*env)->GetMethodID(env, deviceInfoClass, "setDevId", "(Ljava/lang/String;)V");
  108. jstring devIdStr = (*env)->NewStringUTF(env, device->devId);
  109. (*env)->CallVoidMethod(env, obj, setDevIdMethod, devIdStr);
  110. (*env)->DeleteLocalRef(env, devIdStr);
  111. //设置devName
  112. jmethodID setDevNameMethod = (*env)->GetMethodID(env, deviceInfoClass, "setDevName", "(Ljava/lang/String;)V");
  113. jstring devNameStr = (*env)->NewStringUTF(env, device->devName);
  114. (*env)->CallVoidMethod(env, obj, setDevNameMethod, devNameStr);
  115. (*env)->DeleteLocalRef(env, devNameStr);
  116. //发现DeviceType java类,根据其提供的静态getForValue(int)方法获取DeviceType对象
  117. jclass deviceTypeClass = (*env)->FindClass(env, "com/jg/softbus/device/DeviceType");
  118. if (deviceTypeClass == NULL) {
  119. // 处理错误
  120. printf("Cannot found the java class:com/jg/softbus/device/DeviceType");
  121. return NULL;
  122. }
  123. jmethodID getForValueMethod = (*env)->GetStaticMethodID(env, deviceTypeClass, "getForValue", "(I)Lcom/jg/softbus/device/DeviceType;");
  124. if (getForValueMethod == NULL) {
  125. // 处理错误
  126. printf("Cannot found the static method:getForValue(int)");
  127. return NULL;
  128. }
  129. jobject deviceTypeObj = (*env)->CallStaticObjectMethod(env, deviceTypeClass, getForValueMethod, (int)(device->devType));
  130. if (deviceTypeObj == NULL) {
  131. // 处理错误
  132. printf("Cannot found DeviceType. the devType is: %d", device->devType);
  133. return NULL;
  134. }
  135. //设置devType
  136. jmethodID setDevTypeMethod = (*env)->GetMethodID(env, deviceInfoClass, "setDevType", "(Lcom/jg/softbus/device/DeviceType;)V");
  137. (*env)->CallVoidMethod(env, obj, setDevTypeMethod, deviceTypeObj);
  138. (*env)->DeleteLocalRef(env, deviceTypeObj);
  139. //设置在线状态isOnline
  140. // jmethodID setIsOnlineMethod = (*env)->GetMethodID(env, deviceInfoClass, "setOnline", "(Z)V");
  141. // (*env)->CallVoidMethod(env, obj, setIsOnlineMethod, device->isOnline);
  142. //设置地址数addrNum
  143. jmethodID setAddrNumMethod = (*env)->GetMethodID(env, deviceInfoClass, "setAddrNum", "(I)V");
  144. (*env)->CallVoidMethod(env, obj, setAddrNumMethod, device->addrNum);
  145. //发现ConnectionAddr的java类
  146. jclass connectionAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddr");
  147. if (connectionAddrClass == NULL) {
  148. // 处理错误
  149. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddr");
  150. return NULL;
  151. }
  152. //构建一个java ConnectionAddr的数组,数组长度为addrNum
  153. jobjectArray addrArray = (*env)->NewObjectArray(env, device->addrNum, connectionAddrClass, NULL);
  154. //发现com.jg.softbus.network.protocol.ConnectionAddrType java类,根据其提供的静态getForValue(int)方法获取ConnectionAddrType对象
  155. jclass connectionAddrTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddrType");
  156. if (connectionAddrTypeClass == NULL) {
  157. // 处理错误
  158. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddrType");
  159. return NULL;
  160. }
  161. //查找com.jg.softbus.network.protocol.ConnectionAddrInfo
  162. jclass connectionAddrInfoClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddrInfo");
  163. if (connectionAddrInfoClass == NULL) {
  164. // 处理错误
  165. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddrInfo");
  166. return NULL;
  167. }
  168. //查找ConnectionAddrInfo的无参构造函数
  169. jmethodID connectionAddrInfoConstructor = (*env)->GetMethodID(env, connectionAddrInfoClass, "<init>", "()V");
  170. if (connectionAddrInfoConstructor == NULL) {
  171. // 处理错误
  172. printf("Cannot found the constructor:com/jg/softbus/network/protocol/ConnectionAddrInfo");
  173. return NULL;
  174. }
  175. //查找com.jg.softbus.network.protocol.IpAddr的java对象
  176. jclass ipAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/IpAddr");
  177. if(ipAddrClass == NULL){
  178. printf("Cannot found the IpAddr class.");
  179. return NULL;
  180. }
  181. //查找IpAddr带参构造函数,函数结构为:IpAddr(String ip, int port)
  182. jmethodID ipAddrConstructor = (*env)->GetMethodID(env, ipAddrClass, "<init>", "(Ljava/lang/String;I)V");
  183. if(ipAddrConstructor == NULL){
  184. printf("Cannot found the constructor of IpAddr.");
  185. return NULL;
  186. }
  187. //查找com.jg.softbus.network.protocol.BrAddr的java对象
  188. jclass brAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BrAddr");
  189. if(brAddrClass == NULL){
  190. printf("Cannot found the BrAddr class.");
  191. return NULL;
  192. }
  193. //查找BrAddr带参构造函数,函数结构为:BrAddr(String brMac)
  194. jmethodID brAddrConstructor = (*env)->GetMethodID(env, brAddrClass, "<init>", "(Ljava/lang/String)V");
  195. if(brAddrConstructor == NULL){
  196. printf("Cannot found the constructor of BrAddr.");
  197. return NULL;
  198. }
  199. //查找com.jg.softbus.network.protocol.BleAddr的java对象
  200. jclass bleAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BleAddr");
  201. if(bleAddrClass == NULL){
  202. printf("Cannot found the BleAddr class.");
  203. return NULL;
  204. }
  205. //查找BleAddr无参构造函数,函数结构为:BleAddr()
  206. jmethodID bleAddrConstructor = (*env)->GetMethodID(env, bleAddrClass, "<init>", "()V");
  207. if(bleAddrConstructor == NULL){
  208. printf("Cannot found the constructor of BleAddr.");
  209. return NULL;
  210. }
  211. //查找com.jg.softbus.network.protocol.BleProtocolType的java对象
  212. // jclass bleProtocolTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BleProtocolType");
  213. // if(bleProtocolTypeClass == NULL){
  214. // printf("Cannot found the BleProtocolType class.");
  215. // return NULL;
  216. // }
  217. //查找com/jg/softbus/network/protocol/SessionAddr的java对象
  218. // jclass sessionAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/SessionAddr");
  219. // if(sessionAddrClass == NULL){
  220. // printf("Cannot found the SessionAddr class.");
  221. // return NULL;
  222. // }
  223. //查找SessionAddr无参构造函数,函数结构为:SessionAddr()
  224. // jmethodID sessionAddrConstructor = (*env)->GetMethodID(env, sessionAddrClass, "<init>", "()V");
  225. // if(sessionAddrConstructor == NULL){
  226. // printf("Cannot found the constructor of SessionAddr.");
  227. // return NULL;
  228. // }
  229. for (int i = 0; i < device->addrNum; i++) {
  230. //基于无参的构造函数创建ConnectionAddr对象
  231. jmethodID connectionAddrConstructor = (*env)->GetMethodID(env, connectionAddrClass, "<init>", "()V");
  232. jobject addrObj = (*env)->NewObject(env, connectionAddrClass, connectionAddrConstructor);
  233. //获取ConnectionAddrType对象
  234. jmethodID getForValueMethod = (*env)->GetStaticMethodID(env, connectionAddrTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/protocol/ConnectionAddrType;");
  235. if (getForValueMethod == NULL) {
  236. // 处理错误
  237. printf("Cannot found the method:getForValue(int)");
  238. return NULL;
  239. }
  240. //根据device提供的地址类型获取ConnectionAddrType对象
  241. jobject addrTypeObj = (*env)->CallStaticObjectMethod(env, connectionAddrTypeClass, getForValueMethod, device->addr[i].type);
  242. if(addrTypeObj == NULL) {
  243. // 处理错误
  244. printf("Cannot found the ConnectionAddrType. type info: %d", device->addr[i].type);
  245. return NULL;
  246. }
  247. //查找ConnectionAddr的setType(ConnectionAddrType type)方法
  248. jmethodID setTypeMethod = (*env)->GetMethodID(env, connectionAddrClass, "setType", "(Lcom/jg/softbus/network/protocol/ConnectionAddrType;)V");
  249. //设置地址类型到地址对象中
  250. (*env)->CallVoidMethod(env, addrObj, setTypeMethod, addrTypeObj);
  251. (*env)->DeleteLocalRef(env, addrTypeObj);
  252. //无参创建ConnectionAddrInfo的java对象
  253. jobject infoObj = (*env)->NewObject(env, connectionAddrInfoClass, connectionAddrInfoConstructor);
  254. //判断device->addr[i].type的类型,可参考下面代码的Switch方式
  255. switch (device->addr[i].type) {
  256. case CONNECTION_ADDR_WLAN: {
  257. printf("ip=%s,port=%d,", device->addr[i].info.ip.ip, device->addr[i].info.ip.port);
  258. }
  259. case CONNECTION_ADDR_ETH: {
  260. jstring ipstr = (*env)->NewStringUTF(env, device->addr[i].info.ip.ip);
  261. //并使用有参构造函数进行初始化
  262. jobject ipAddrObj = (*env)->NewObject(env, ipAddrClass, ipAddrConstructor, ipstr, device->addr[i].info.ip.port);
  263. //将地址对象写入ConnectionAddrInfo对象中
  264. jmethodID setIpMethod = (*env)->GetMethodID(env, connectionAddrClass, "setIp", "(Lcom/jg/softbus/network/protocol/IpAddr;)V");
  265. //设置地址类型到地址对象中
  266. (*env)->CallVoidMethod(env, addrObj, setIpMethod, ipAddrObj);
  267. (*env)->DeleteLocalRef(env, ipAddrObj);
  268. break;
  269. }
  270. case CONNECTION_ADDR_BR: {
  271. jstring brMacStr = (*env)->NewStringUTF(env, device->addr[i].info.br.brMac);
  272. //并使用有参构造函数进行初始化
  273. jobject brAddrObj = (*env)->NewObject(env, brAddrClass, brAddrConstructor, brMacStr);
  274. //将地址对象写入ConnectionAddrInfo对象中
  275. jmethodID setBrMethod = (*env)->GetMethodID(env, connectionAddrClass, "setBr", "(Lcom/jg/softbus/network/protocol/BrAddr;)V");
  276. //设置地址类型到地址对象中
  277. (*env)->CallVoidMethod(env, addrObj, setBrMethod, brAddrObj);
  278. (*env)->DeleteLocalRef(env, brAddrObj);
  279. (*env)->DeleteLocalRef(env, brMacStr);
  280. break;
  281. }
  282. case CONNECTION_ADDR_BLE: {
  283. //并使用构造函数进行初始化
  284. jobject bleAddrObj = (*env)->NewObject(env, bleAddrClass, bleAddrConstructor);
  285. // //获取BleProtocolType对象
  286. // jmethodID getForValueMethod2 = (*env)->GetStaticMethodID(env, bleProtocolTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/protocol/BleProtocolType;");
  287. // if (getForValueMethod2 == NULL) {
  288. // // 处理错误
  289. // printf("Cannot found the method:getForValue(int)");
  290. // return NULL;
  291. // }
  292. // //根据device提供的bleprotocol类型获取BleProtocolType对象
  293. // jobject bleProtocolTypeObj = (*env)->CallStaticObjectMethod(env, bleProtocolTypeClass, getForValueMethod2, device->addr[i].info.ble.protocol);
  294. // if(bleProtocolTypeObj == NULL) {
  295. // // 处理错误
  296. // printf("Cannot found the BleProtocolType. type info: %d", device->addr[i].info.ble.protocol);
  297. // return NULL;
  298. // }
  299. // //将地址对象写入ConnectionAddrInfo对象中
  300. // jmethodID setProtocolMethod = (*env)->GetMethodID(env, bleAddrClass, "setProtocol", "(Lcom/jg/softbus/network/protocol/BleProtocolType;)V");
  301. // (*env)->CallVoidMethod(env, bleAddrObj, setProtocolMethod, bleProtocolTypeObj);
  302. // (*env)->DeleteLocalRef(env, bleProtocolTypeObj);
  303. //setbleMac
  304. jmethodID setBleMacMethod = (*env)->GetMethodID(env, bleAddrClass, "setBleMac", "(Ljava/lang/String)V");
  305. jstring bleMac = (*env)->NewStringUTF(env, device->addr[i].info.ble.bleMac);
  306. (*env)->CallVoidMethod(env, bleAddrObj, setBleMacMethod, bleMac);
  307. (*env)->DeleteLocalRef(env, bleMac);
  308. //setUdidHash
  309. jmethodID setUdidHashMethod = (*env)->GetMethodID(env, bleAddrClass, "setUdidHash", "([B)V");
  310. size_t len = UDID_HASH_LEN / sizeof(uint8_t);
  311. jbyteArray byteArray = (*env)->NewByteArray(env, len);
  312. (*env)->SetByteArrayRegion(env, byteArray, 0, len, (const jbyte*)device->addr[i].info.ble.udidHash);
  313. // 将udidHash转换为整数
  314. // int hash_value = 0;
  315. // for (int k = 0; k < UDID_HASH_LEN; k++) {
  316. // hash_value = (hash_value << 8) + device->addr[i].info.ble.udidHash[k];
  317. // }
  318. (*env)->CallVoidMethod(env, bleAddrObj, setUdidHashMethod, byteArray);
  319. (*env)->DeleteLocalRef(env, byteArray);
  320. // //setPsm
  321. // jmethodID setPsmMethod = (*env)->GetMethodID(env, bleAddrClass, "setPsm", "(I)V");
  322. // (*env)->CallVoidMethod(env, bleAddrObj, setPsmMethod, device->addr[i].info.ble.psm);
  323. //将地址对象写入ConnectionAddrInfo对象中
  324. jmethodID setBleMethod = (*env)->GetMethodID(env, connectionAddrClass, "setBle", "(Lcom/jg/softbus/network/protocol/BrAddr;)V");
  325. (*env)->CallVoidMethod(env, addrObj, setBleMethod, bleAddrObj);
  326. (*env)->DeleteLocalRef(env, bleAddrObj);
  327. printf("bleMac=%s,", device->addr[i].info.ble.bleMac);
  328. break;
  329. }
  330. // case CONNECTION_ADDR_SESSION:
  331. // //创建SessionAddr对象
  332. // jobject sessionAddrObj = (*env)->NewObject(env, sessionAddrClass, sessionAddrConstructor);
  333. // //获取void setSessionId(Integer sessionId)方法
  334. // jmethodID setSessionIdMethod = (*env)->GetMethodID(env, sessionAddrClass, "setSessionId", "(I)V");
  335. // //将device中的sessionId调用方法设置到sessionAddrObj中
  336. // (*env)->CallVoidMethod(env, sessionAddrObj, setSessionIdMethod, device->addr[i].info.session.sessionId);
  337. // //获取void setChannelId(Integer channelId)方法
  338. // jmethodID setChannelIdMethod = (*env)->GetMethodID(env, sessionAddrClass, "setChannelId", "(I)V");
  339. // //将device中的channelId调用方法设置到sessionAddrObj中
  340. // (*env)->CallVoidMethod(env, sessionAddrObj, setChannelIdMethod, device->addr[i].info.session.channelId);
  341. // //获取void setType(String type)方法
  342. // jmethodID setTypeMethod = (*env)->GetMethodID(env, sessionAddrClass, "setType", "(Ljava/lang/String;)V");
  343. // //将device中的type调用方法设置到sessionAddrObj中
  344. // jstring sessionTypeStr = (*env)->NewStringUTF(env, device->addr[i].info.session.type);
  345. // (*env)->CallVoidMethod(env, sessionAddrObj, setTypeMethod, sessionTypeStr);
  346. // jmethodID setSessionMethod = (*env)->GetMethodID(env, connectionAddrClass, "setSession", "(Lcom/jg/softbus/network/protocol/SessionAddr;)V");
  347. // (*env)->CallVoidMethod(env, addrObj, setSessionMethod, sessionAddrObj);
  348. // (*env)->DeleteLocalRef(env, sessionTypeStr);
  349. // (*env)->DeleteLocalRef(env, sessionAddrObj);
  350. // printf("sessionId=%d, channelId=%d, type=%d", device->addr[i].info.session.sessionId, device->addr[i].info.session.channelId,device->addr[i].info.session.type);
  351. // break;
  352. default:
  353. break;
  354. }
  355. //获取ConnectionAddr中的void setPeerUid(String peerUid)方法,将device中的peerUid放入到addrObj中
  356. jmethodID setPeerUidMethod = (*env)->GetMethodID(env, connectionAddrClass, "setPeerUid", "(Ljava/lang/String;)V");
  357. jstring peerUidStr = (*env)->NewStringUTF(env, device->addr[i].peerUid);
  358. (*env)->CallVoidMethod(env, addrObj, setPeerUidMethod, peerUidStr);
  359. (*env)->DeleteLocalRef(env, peerUidStr);
  360. (*env)->SetObjectArrayElement(env, addrArray, i, addrObj);
  361. (*env)->DeleteLocalRef(env, addrObj);
  362. }
  363. //查找void setAddr(ConnectionAddr[] addr)方法并将addrArray对象设置到obj对象中
  364. jmethodID setAddrMethod = (*env)->GetMethodID(env, deviceInfoClass, "setAddr", "([Lcom/jg/softbus/network/protocol/ConnectionAddr;)V");
  365. (*env)->CallVoidMethod(env, obj, setAddrMethod, addrArray);
  366. (*env)->DeleteLocalRef(env, addrArray);
  367. //查找void setCapabilityBitmapNum(Integer capabilityBitmapNum)方法
  368. jmethodID setCapabilityBitmapNumMethod = (*env)->GetMethodID(env, deviceInfoClass, "setCapabilityBitmapNum", "(I)V");
  369. (*env)->CallVoidMethod(env, obj, setCapabilityBitmapNumMethod, device->capabilityBitmapNum);
  370. //查找void setCapabilityBitmap(String[] capabilityBitmap)方法
  371. jmethodID setCapabilityBitmapMethod = (*env)->GetMethodID(env, deviceInfoClass, "setCapabilityBitmap", "([Ljava/lang/String;)V");
  372. //构建jstring的数组对象,长度为addrNum的数值
  373. jobjectArray sArray = (*env)->NewObjectArray(env, device->addrNum, (*env)->FindClass(env, "java/lang/String"), NULL);
  374. for (int i = 0; i < device->addrNum; i++) {
  375. char* cbitmap_str;
  376. sprintf(cbitmap_str, "0x%x", device->capabilityBitmap[i]);
  377. //将cbitmap_str放入到jstringArray中
  378. jstring addrObj = (*env)->NewStringUTF(env, cbitmap_str);
  379. (*env)->SetObjectArrayElement(env, sArray, i, addrObj);
  380. (*env)->DeleteLocalRef(env, addrObj);
  381. printf("\t\tcapabilityBitmap[%d]=0x%x\n", i + 1, device->capabilityBitmap[i]);
  382. }
  383. (*env)->CallVoidMethod(env, obj, setCapabilityBitmapMethod, sArray);
  384. (*env)->DeleteLocalRef(env, sArray);
  385. //查找void setCustData(String custData)方法
  386. jmethodID setCustDataMethod = (*env)->GetMethodID(env, deviceInfoClass, "setCustData", "(Ljava/lang/String;)V");
  387. //获取device的custData放入到obj中
  388. jstring custDataObj = (*env)->NewStringUTF(env, device->custData);
  389. (*env)->CallVoidMethod(env, obj, setCustDataMethod, custDataObj);
  390. (*env)->DeleteLocalRef(env, custDataObj);
  391. //获取void setRange(Integer range)方法
  392. // jmethodID setRangeMethod = (*env)->GetMethodID(env, deviceInfoClass, "setRange", "(I)V");
  393. // (*env)->CallVoidMethod(env, obj, setRangeMethod, device->range);
  394. }
  395. /**
  396. * 设备发现回调
  397. */
  398. void DeviceFound(const DeviceInfo *device)
  399. {
  400. // 获取JNIEnv环境
  401. JNIEnv *env;
  402. // 将当前线程附加到Java虚拟机
  403. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  404. // 获取当前对象的类引用
  405. jclass nativeLibClass = (*env)->GetObjectClass(env, globalRef);
  406. // 获取当前对象的publishSuccess方法的ID
  407. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onDeviceFound", "(Lcom/jg/softbus/device/DeviceInfo;)V");
  408. //转换DeviceInfo为java对应的对象
  409. jobject deviceObj = DeviceInfoToJavaObject(env, device);
  410. if(deviceObj == NULL) {
  411. return;
  412. }
  413. //调用回调方法执行
  414. (*env)->CallVoidMethod(env, globalRef, callbackMethod, deviceObj);
  415. //删除本地对象
  416. (*env)->DeleteLocalRef(env, deviceObj);
  417. //将当前线程从java虚拟机中移除
  418. (*jvm)->DetachCurrentThread(jvm);
  419. }
  420. /**
  421. * 发现成功回调
  422. */
  423. static void DiscoverySuccess(int subscribeId)
  424. {
  425. // 获取JNIEnv环境
  426. JNIEnv *env;
  427. // 将当前线程附加到Java虚拟机
  428. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  429. // 获取当前对象的类引用
  430. jclass nativeLibClass = (*env)->GetObjectClass(env, globalRef);
  431. // 获取当前对象的publishSuccess方法的ID
  432. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onDiscoverySuccess", "(I)V");
  433. //调用回调方法执行
  434. (*env)->CallVoidMethod(env, globalRef, callbackMethod, subscribeId);
  435. //将当前线程从java虚拟机中移除
  436. (*jvm)->DetachCurrentThread(jvm);
  437. printf("<DiscoverySuccess>CB: discover subscribeId=%d\n", subscribeId);
  438. }
  439. /**
  440. * 发现失败回调
  441. */
  442. static void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason)
  443. {
  444. // 获取JNIEnv环境
  445. JNIEnv *env;
  446. // 将当前线程附加到Java虚拟机
  447. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  448. // 获取当前对象的类引用
  449. jclass nativeLibClass = (*env)->GetObjectClass(env, globalRef);
  450. // 获取当前对象的publishSuccess方法的ID
  451. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onDiscoverFailed", "(II)V");
  452. // 调用当前对象的非静态方法
  453. (*env)->CallVoidMethod(env, globalRef, callbackMethod, subscribeId, (int)reason);
  454. //将当前线程从java虚拟机中移除
  455. (*jvm)->DetachCurrentThread(jvm);
  456. printf("<DiscoveryFailed>CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason);
  457. }
  458. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_SubscribeService_discoveryStart
  459. (JNIEnv *env, jobject obj, jstring pkgName, jobject subscribeInfo, jobject callback) {
  460. // 保存Java虚拟机的引用
  461. (*env)->GetJavaVM(env, &jvm);
  462. globalRef = (*env)->NewGlobalRef(env, callback);
  463. const char *pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  464. SubscribeInfo info;
  465. int ret = ParseSubscribeInfo(env, subscribeInfo, &info);
  466. if(ret != 0) {
  467. // 尝试进行一些可能会抛出异常的操作
  468. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  469. // 如果找不到异常类,则抛出异常
  470. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  471. jthrowable exception = (*env)->NewObject(env,exceptionClass, exceptionConstructor);
  472. // 抛出异常
  473. (*env)->ThrowNew(env, exceptionClass, "SubscribeInfo object error.");
  474. //解析失败无法继续操作
  475. return -1;
  476. }
  477. IDiscoveryCallback cb = {
  478. .OnDeviceFound = DeviceFound,
  479. .OnDiscoverFailed = DiscoveryFailed,
  480. .OnDiscoverySuccess = DiscoverySuccess
  481. };
  482. ret = StartDiscovery(pkgNameStr, &info, &cb);
  483. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  484. return ret;
  485. }
  486. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_SubscribeService_stopDiscovery
  487. (JNIEnv * env, jobject obj, jstring pkgName, jint subscribeId) {
  488. const char* pkgName_str = (*env)->GetStringUTFChars(env, pkgName, NULL);
  489. int ret = StopDiscovery(pkgName_str, subscribeId);
  490. if (ret) {
  491. // 尝试进行一些可能会抛出异常的操作
  492. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  493. // 如果找不到异常类,则抛出异常
  494. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  495. jthrowable exception = (*env)->NewObject(env, exceptionClass, exceptionConstructor);
  496. // 抛出异常
  497. (*env)->ThrowNew(env, exceptionClass, "stop discovery error.");
  498. printf("StopDiscovery fail:%d\n", ret);
  499. }
  500. printf("<stopDiscovery>stop discover pkgName=%s, subscribeId=%d\n", pkgName_str, subscribeId);
  501. (*env)->ReleaseStringUTFChars(env, pkgName, pkgName_str);
  502. return 0;
  503. }
  504. static void discoverResult(int32_t refreshId, RefreshResult reason) {
  505. // 获取JNIEnv环境
  506. JNIEnv *env;
  507. // 将当前线程附加到Java虚拟机
  508. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  509. // 获取当前对象的类引用
  510. jclass nativeLibClass = (*env)->GetObjectClass(env, globalRef);
  511. // 获取当前对象的publishSuccess方法的ID
  512. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onDiscoverResult", "(II)V");
  513. // 调用当前对象的非静态方法
  514. (*env)->CallVoidMethod(env, globalRef, callbackMethod, refreshId, (int)reason);
  515. // 将当前线程从Java虚拟机分离
  516. (*jvm)->DetachCurrentThread(jvm);
  517. printf("<DiscoveryResult>CB: discover subscribeId=%d failed, reason=%d\n", refreshId, (int)reason);
  518. }
  519. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_SubscribeService_refreshLNN
  520. (JNIEnv *env, jobject obj, jstring pkgName, jobject subscribeInfo, jobject callback){
  521. // 保存Java虚拟机的引用
  522. (*env)->GetJavaVM(env, &jvm);
  523. globalRef = (*env)->NewGlobalRef(env, callback);
  524. const char *pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  525. SubscribeInfo info;
  526. int ret = ParseSubscribeInfo(env, subscribeInfo, &info);
  527. if(ret != 0) {
  528. // 尝试进行一些可能会抛出异常的操作
  529. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  530. // 如果找不到异常类,则抛出异常
  531. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  532. jthrowable exception = (*env)->NewObject(env, exceptionClass, exceptionConstructor);
  533. // 抛出异常
  534. (*env)->ThrowNew(env, exceptionClass, "SubscribeInfo object error.");
  535. //解析失败无法继续操作
  536. return -1;
  537. }
  538. IRefreshCallback cb = {
  539. .OnDeviceFound = DeviceFound,
  540. .OnDiscoverResult = discoverResult,
  541. };
  542. ret = RefreshLNN(pkgNameStr, &info, &cb);
  543. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  544. return ret;
  545. }
  546. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_SubscribeService_stopRefreshLNN
  547. (JNIEnv * env, jobject obj, jstring pkgName, jint refreshId) {
  548. const char* pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  549. // 停止刷新LNN
  550. int ret = StopRefreshLNN(pkgNameStr, refreshId);
  551. if (ret) {
  552. // 尝试进行一些可能会抛出异常的操作
  553. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  554. // 如果找不到异常类,则抛出异常
  555. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  556. jthrowable exception = (*env)->NewObject(env, exceptionClass, exceptionConstructor);
  557. // 抛出异常
  558. (*env)->ThrowNew(env, exceptionClass, "stop refreshLNN error.");
  559. printf("stop refreshLNN fail:%d\n", ret);
  560. }
  561. // 释放pkgNameStr
  562. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  563. return ret;
  564. }
  565. JNIEXPORT jobjectArray JNICALL Java_com_jg_softbus_naservice_SubscribeService_findDeviceNodes
  566. (JNIEnv *env, jobject obj, jstring pkgName) {
  567. const char* pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  568. NodeBasicInfo **dev = NULL;
  569. int ret, num;
  570. ret = GetAllNodeDeviceInfo(pkgNameStr, dev, &num);
  571. if (ret) {
  572. // 尝试进行一些可能会抛出异常的操作
  573. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  574. // 如果找不到异常类,则抛出异常
  575. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  576. jthrowable exception = (*env)->NewObject(env, exceptionClass, exceptionConstructor);
  577. // 抛出异常
  578. (*env)->ThrowNew(env, exceptionClass, "get online device error.");
  579. printf("GetAllNodeDeviceInfo fail:%d\n", ret);
  580. return NULL;
  581. }
  582. jclass nodeClass = (*env)->FindClass(env, "com/jg/softbus/network/node/NodeBasicInfo");
  583. // 获取构造函数
  584. jmethodID nodeConstructor = (*env)->GetMethodID(env, nodeClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;S)V");
  585. jobjectArray json_nodes = (*env)->NewObjectArray(env, num, nodeClass, NULL);
  586. printf("<GetAllNodeDeviceInfo>return %d Node\n", num);
  587. for (int i = 0; i < num; i++) {
  588. jstring networkId = (*env)->NewStringUTF(env, dev[i]->networkId);
  589. jstring deviceName = (*env)->NewStringUTF(env, dev[i]->deviceName);
  590. //构造node对象
  591. jobject node = (*env)->NewObject(env, nodeClass, nodeConstructor, networkId, deviceName, dev[i]->deviceTypeId);
  592. (*env)->SetObjectArrayElement(env, json_nodes, i, node);
  593. //释放字符串资源
  594. (*env)->DeleteLocalRef(env, networkId);
  595. (*env)->DeleteLocalRef(env, deviceName);
  596. (*env)->DeleteLocalRef(env, node);
  597. printf("<num %d>deviceName=%s\n", i + 1, dev[i]->deviceName);
  598. printf("\tnetworkId=%s\n", dev[i]->networkId);
  599. printf("\tdeviceTypeId=%d\n", dev[i]->deviceTypeId);
  600. }
  601. // 释放pkgNameStr
  602. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  603. return json_nodes;
  604. }
  605. JNIEXPORT void JNICALL Java_com_jg_softbus_naservice_SubscribeService_releaseDeviceNodes
  606. (JNIEnv *env, jobject obj, jobjectArray nodes) {
  607. // 释放创建的节点集合对象
  608. (*env)->DeleteLocalRef(env, nodes);
  609. }
  610. JNIEXPORT void JNICALL Java_com_jg_softbus_naservice_SubscribeService_destroy
  611. (JNIEnv *env, jobject obj) {
  612. // 删除全局引用
  613. (*env)->DeleteGlobalRef(env, globalRef);
  614. free(jvm);
  615. }