SoftbusLNN.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <jni.h>
  5. #include "securec.h"
  6. #include "softbus_bus_center.h"
  7. #include "com_jg_softbus_naservice_LNNService.h"
  8. static INodeStateCb g_stateCb;
  9. // 全局变量,用于保存Java虚拟机的引用
  10. JavaVM *jvm;
  11. //全局引用变量,用于保存java对象
  12. jobject lNNCallback;
  13. jobject leaveCallback;
  14. jobject nodeStateCb;
  15. int parseConnectAddr(JNIEnv *env, jobject targetAddr, ConnectionAddr *addr) {
  16. jclass addrClass = (*env)->GetObjectClass(env, targetAddr);
  17. //地址类型
  18. jmethodID getAddrTypeMethod = (*env)->GetMethodID(env, addrClass, "getType", "()Lcom/jg/softbus/network/protocol/ConnectionAddrType;");
  19. jobject addrType = (*env)->CallObjectMethod(env, targetAddr, getAddrTypeMethod);
  20. jclass addrTypeClass = (*env)->GetObjectClass(env, addrType);
  21. jmethodID getModeMethod = (*env)->GetMethodID(env, addrTypeClass, "valueOf", "()I");
  22. jint type = (*env)->CallIntMethod(env, addrType, getModeMethod);
  23. addr->type = type;
  24. printf("addr->peerUid:%d\n", addr->type);
  25. (*env)->DeleteLocalRef(env, addrType);
  26. //开始解析写入info对象
  27. jmethodID getInfoMethod = (*env)->GetMethodID(env, addrClass, "getInfo", "()Lcom/jg/softbus/network/protocol/ConnectionAddrInfo;");
  28. jobject addrInfo = (*env)->CallObjectMethod(env, targetAddr, getInfoMethod);
  29. if(addrInfo != NULL) {
  30. //解析infoclass
  31. jclass addrInfoClass = (*env)->GetObjectClass(env, addrInfo);
  32. //获取getBr
  33. jmethodID getBrMethod = (*env)->GetMethodID(env, addrInfoClass, "getBr", "()Lcom/jg/softbus/network/protocol/BrAddr;");
  34. jobject brAddr = (*env)->CallObjectMethod(env, addrInfo, getBrMethod);
  35. if (brAddr != NULL) {
  36. jclass brAddrClass = (*env)->GetObjectClass(env, brAddr);
  37. //获取getBrMac
  38. jmethodID getBrMacMethod = (*env)->GetMethodID(env, brAddrClass, "getBrMac", "()Ljava/lang/String;");
  39. jstring brMac = (*env)->CallObjectMethod(env, brAddr, getBrMacMethod);
  40. const char *brMacStr = (*env)->GetStringUTFChars(env, brMac, 0);
  41. if (brMacStr != NULL) {
  42. // 获取字符串长度
  43. jsize length = (*env)->GetStringUTFLength(env, brMac);
  44. // 将字符串转换为char数组
  45. char brMacArray[length+1];
  46. for (int i = 0; i < length; i++) {
  47. brMacArray[i] = brMacStr[i];
  48. }
  49. // 添加字符串结束符
  50. brMacArray[length] = '\0';
  51. strcpy(addr->info.br.brMac, brMacArray);
  52. printf("addr->info.br.brMac:%s\n", brMacStr);
  53. }
  54. (*env)->ReleaseStringUTFChars(env, brMac, brMacStr);
  55. }
  56. (*env)->DeleteLocalRef(env, brAddr);
  57. //获取BleAddr
  58. jmethodID getBleMethod = (*env)->GetMethodID(env, addrInfoClass, "getBle", "()Lcom/jg/softbus/network/protocol/BleAddr;");
  59. jobject bleAddr = (*env)->CallObjectMethod(env, addrInfo, getBleMethod);
  60. if(bleAddr != NULL) {
  61. jclass bleAddrClass = (*env)->GetObjectClass(env, bleAddr);
  62. //获取getProtocol
  63. jmethodID getProtocolMethod = (*env)->GetMethodID(env, bleAddrClass, "getProtocol", "()Lcom/jg/softbus/network/protocol/BleProtocolType;");
  64. // jobject protocol = (*env)->CallObjectMethod(env, bleAddr, getProtocolMethod);
  65. // if(protocol != NULL) {
  66. // jclass protocolClass = (*env)->GetObjectClass(env, protocol);
  67. // jmethodID getProtocolValueMethod = (*env)->GetMethodID(env, protocolClass, "valueOf", "()I");
  68. // jint protocolValue = (*env)->CallIntMethod(env, protocol, getProtocolValueMethod);
  69. // addr->info.ble.protocol = protocolValue;
  70. // printf("addr->info.ble.protocol:%d\n", addr->info.ble.protocol);
  71. // }
  72. // (*env)->DeleteLocalRef(env, protocol);
  73. //获取bleMac
  74. jmethodID getBleMacMethod = (*env)->GetMethodID(env, bleAddrClass, "getBleMac", "()Ljava/lang/String;");
  75. jstring bleMac = (*env)->CallObjectMethod(env, bleAddr, getBleMacMethod);
  76. const char *bleMacStr = (*env)->GetStringUTFChars(env, bleMac, 0);
  77. if (bleMac != NULL) {
  78. // 获取字符串长度
  79. jsize length = (*env)->GetStringUTFLength(env, bleMac);
  80. // 将字符串转换为char数组
  81. char bleMacArray[length+1];
  82. for (int i = 0; i < length; i++) {
  83. bleMacArray[i] = bleMacStr[i];
  84. }
  85. // 添加字符串结束符
  86. bleMacArray[length] = '\0';
  87. strcpy(addr->info.ble.bleMac, bleMacArray);
  88. printf("addr->info.ble.bleMac:%s\n", bleMacStr);
  89. }
  90. (*env)->ReleaseStringUTFChars(env, bleMac, bleMacStr);
  91. //获取udidHash
  92. jmethodID getUdidHashMethod = (*env)->GetMethodID(env, bleAddrClass, "getUdidHash", "()[B");
  93. jobject udidHashValue = (*env)->CallObjectMethod(env, bleAddr, getUdidHashMethod);
  94. if(udidHashValue != NULL) {
  95. jclass udidHashValueClass = (*env)->GetObjectClass(env, udidHashValue);
  96. jobject classObj = (*env)->ToReflectedMethod(env, udidHashValueClass, 0, 0);
  97. jclass classClass = (*env)->GetObjectClass(env, classObj);
  98. jmethodID mid = (*env)->GetMethodID(env, classClass, "getName", "()Ljava/lang/String;");
  99. jstring className = (*env)->CallObjectMethod(env, classObj, mid);
  100. const char *classSig = (*env)->GetStringUTFChars(env, className, NULL);
  101. if (classSig[0] == '[') {
  102. jsize length = (*env)->GetArrayLength(env, udidHashValue);
  103. jbyteArray array = (*env)->NewByteArray(env, length);
  104. // 将数据复制到jArray中
  105. jbyte *buffer = (*env)->GetByteArrayElements(env, array, NULL);
  106. if (buffer == NULL) {
  107. return -1; // 返回null表示失败
  108. }
  109. for (jsize i = 0; i < length; i++) {
  110. // 从jobject中获取元素并复制到buffer中
  111. buffer[i] = (uint8_t)((*env)->CallIntMethod(env, udidHashValue, (*env)->GetMethodID(env, udidHashValueClass, "get", "(I)B"), i));
  112. }
  113. (*env)->ReleaseByteArrayElements(env, array, buffer, 0); // 释放本地引用
  114. // 获取jbyteArray的指针和长度
  115. jbyte *elements = (*env)->GetByteArrayElements(env, array, NULL);
  116. // 创建uint8_t数组
  117. uint8_t udidHash[length];
  118. // 将数据复制到uint8_t数组中
  119. for (jsize i = 0; i < length; i++) {
  120. udidHash[i] = (uint8_t)elements[i];
  121. printf("addr->info.ble.udidHash:%u", udidHash[i]);
  122. }
  123. printf("\n");
  124. (*env)->ReleaseByteArrayElements(env, array, elements, 0);
  125. memcpy(addr->info.ble.udidHash, udidHash, sizeof(udidHash));
  126. free(udidHash);
  127. }
  128. }
  129. (*env)->DeleteLocalRef(env, udidHashValue);
  130. //psm处理
  131. // jmethodID getPsmMethod = (*env)->GetMethodID(env, bleAddrClass, "getPsm", "()I");
  132. // jint psm = (*env)->CallIntMethod(env, bleAddr, getPsmMethod);
  133. // addr->info.ble.psm = psm;
  134. }
  135. //释放bleAddr引用
  136. (*env)->DeleteLocalRef(env, bleAddr);
  137. jmethodID getIpMethod = (*env)->GetMethodID(env, addrInfoClass, "getIp", "()Lcom/jg/softbus/network/protocol/IpAddr;");
  138. jobject ipAddr = (*env)->CallObjectMethod(env, addrInfo, getIpMethod);
  139. if(ipAddr != NULL) {
  140. jclass ipAddrClass = (*env)->GetObjectClass(env, ipAddr);
  141. //获取ip属性
  142. jmethodID getIpMethod = (*env)->GetMethodID(env, ipAddrClass, "getIp", "()Ljava/lang/String;");
  143. jstring ip = (*env)->CallObjectMethod(env, ipAddr, getIpMethod);
  144. const char *ipStr = (*env)->GetStringUTFChars(env, ip, 0);
  145. if (ip != NULL) {
  146. // 获取字符串长度
  147. jsize length = (*env)->GetStringUTFLength(env, ip);
  148. // 将字符串转换为char数组
  149. char ipArray[length+1];
  150. for (int i = 0; i < length; i++) {
  151. ipArray[i] = ipStr[i];
  152. }
  153. // 添加字符串结束符
  154. ipArray[length] = '\0';
  155. strcpy(addr->info.ip.ip, ipArray);
  156. printf("addr->info.ip.ip:%s\n", addr->info.ip.ip);
  157. }
  158. (*env)->ReleaseStringUTFChars(env, ip, ipStr);
  159. //获取端口port
  160. jmethodID getPortMethod = (*env)->GetMethodID(env, ipAddrClass, "getPort", "()I");
  161. jint port = (*env)->CallIntMethod(env, ipAddr, getPortMethod);
  162. addr->info.ip.port = port;
  163. printf("addr->info.ip.port:%d\n", addr->info.ip.port);
  164. }
  165. //释放ipAddr引用
  166. (*env)->DeleteLocalRef(env, ipAddr);
  167. //session处理
  168. // jmethodID getSessionMethod = (*env)->GetMethodID(env, addrInfoClass, "getSession", "()Lcom/jg/softbus/network/protocol/SessionAddr;");
  169. // jobject sessionAddr = (*env)->CallObjectMethod(env, addrInfo, getSessionMethod);
  170. // if(sessionAddr != NULL) {
  171. // jclass sessionClass = (*env)->GetObjectClass(env, sessionAddr);
  172. // //获取sessionId属性
  173. // jmethodID getSessionIdMethod = (*env)->GetMethodID(env, sessionClass, "getSessionId", "()I");
  174. // jint sessionId = (*env)->CallIntMethod(env, sessionAddr, getSessionIdMethod);
  175. // addr->info.session.sessionId = sessionId;
  176. // printf("addr->info.session.sessionId:%d\n", addr->info.session.sessionId);
  177. // //获取channelId
  178. // jmethodID getChannelIdMethod = (*env)->GetMethodID(env, sessionClass, "getChannelId", "()I");
  179. // jint channelId = (*env)->CallIntMethod(env, sessionAddr, getChannelIdMethod);
  180. // addr->info.session.channelId = channelId;
  181. // printf("addr->info.session.channelId:%d\n", addr->info.session.channelId);
  182. // //获取type属性
  183. // jmethodID getTypeMethod = (*env)->GetMethodID(env, sessionClass, "getType", "()I");
  184. // jint type = (*env)->CallIntMethod(env, sessionAddr, getTypeMethod);
  185. // addr->info.session.type = type;
  186. // printf("addr->info.session.type:%d\n", addr->info.session.type);
  187. // }
  188. // //释放sessionAddr引用
  189. // (*env)->DeleteLocalRef(env, sessionAddr);
  190. // }
  191. //开始解析peerUid
  192. jmethodID getPeerUidMethod = (*env)->GetMethodID(env, addrClass, "getPeerUid", "()Ljava/lang/String;");
  193. jstring peerUid = (*env)->CallObjectMethod(env, targetAddr, getPeerUidMethod);
  194. const char *peerUidStr = (*env)->GetStringUTFChars(env, peerUid, 0);
  195. if (peerUidStr != NULL) {
  196. // 获取字符串长度
  197. jsize length = (*env)->GetStringUTFLength(env, peerUid);
  198. // 将字符串转换为char数组
  199. char cArray[length+1];
  200. for (int i = 0; i < length; i++) {
  201. cArray[i] = peerUidStr[i];
  202. }
  203. // 添加字符串结束符
  204. cArray[length] = '\0';
  205. strcpy(addr->peerUid, cArray);
  206. printf("addr->peerUid:%s\n", addr->peerUid);
  207. }
  208. // 释放C字符串的内存
  209. (*env)->ReleaseStringUTFChars(env, peerUid, peerUidStr);
  210. return 0;
  211. }
  212. return -1;
  213. }
  214. /**
  215. * 将c结构ConnectionAddr转换为java的object对象
  216. */
  217. jobject parseAddrToJava(JNIEnv *env, const ConnectionAddr *addr) {
  218. //发现ConnectionAddr的java类
  219. jclass connectionAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddr");
  220. if (connectionAddrClass == NULL) {
  221. // 处理错误
  222. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddr");
  223. return NULL;
  224. }
  225. //发现com.jg.softbus.network.protocol.ConnectionAddrType java类,根据其提供的静态getForValue(int)方法获取ConnectionAddrType对象
  226. jclass connectionAddrTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddrType");
  227. if (connectionAddrTypeClass == NULL) {
  228. // 处理错误
  229. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddrType");
  230. return NULL;
  231. }
  232. //查找com.jg.softbus.network.protocol.ConnectionAddrInfo
  233. jclass connectionAddrInfoClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/ConnectionAddrInfo");
  234. if (connectionAddrInfoClass == NULL) {
  235. // 处理错误
  236. printf("Cannot found the class:com/jg/softbus/network/protocol/ConnectionAddrInfo");
  237. return NULL;
  238. }
  239. //查找ConnectionAddrInfo的无参构造函数
  240. jmethodID connectionAddrInfoConstructor = (*env)->GetMethodID(env, connectionAddrInfoClass, "<init>", "()V");
  241. if (connectionAddrInfoConstructor == NULL) {
  242. // 处理错误
  243. printf("Cannot found the constructor:com/jg/softbus/network/protocol/ConnectionAddrInfo");
  244. return NULL;
  245. }
  246. //查找com.jg.softbus.network.protocol.IpAddr的java对象
  247. jclass ipAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/IpAddr");
  248. if(ipAddrClass == NULL){
  249. printf("Cannot found the IpAddr class.");
  250. return NULL;
  251. }
  252. //查找IpAddr带参构造函数,函数结构为:IpAddr(String ip, int port)
  253. jmethodID ipAddrConstructor = (*env)->GetMethodID(env, ipAddrClass, "<init>", "(Ljava/lang/String;I)V");
  254. if(ipAddrConstructor == NULL){
  255. printf("Cannot found the constructor of IpAddr.");
  256. return NULL;
  257. }
  258. //查找com.jg.softbus.network.protocol.BrAddr的java对象
  259. jclass brAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BrAddr");
  260. if(brAddrClass == NULL){
  261. printf("Cannot found the BrAddr class.");
  262. return NULL;
  263. }
  264. //查找BrAddr带参构造函数,函数结构为:BrAddr(String brMac)
  265. jmethodID brAddrConstructor = (*env)->GetMethodID(env, brAddrClass, "<init>", "(Ljava/lang/String)V");
  266. if(brAddrConstructor == NULL){
  267. printf("Cannot found the constructor of BrAddr.");
  268. return NULL;
  269. }
  270. //查找com.jg.softbus.network.protocol.BleAddr的java对象
  271. jclass bleAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BleAddr");
  272. if(bleAddrClass == NULL){
  273. printf("Cannot found the BleAddr class.");
  274. return NULL;
  275. }
  276. //查找BleAddr无参构造函数,函数结构为:BleAddr()
  277. jmethodID bleAddrConstructor = (*env)->GetMethodID(env, bleAddrClass, "<init>", "()V");
  278. if(bleAddrConstructor == NULL){
  279. printf("Cannot found the constructor of BleAddr.");
  280. return NULL;
  281. }
  282. //查找com.jg.softbus.network.protocol.BleProtocolType的java对象
  283. // jclass bleProtocolTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/BleProtocolType");
  284. // if(bleProtocolTypeClass == NULL){
  285. // printf("Cannot found the BleProtocolType class.");
  286. // return NULL;
  287. // }
  288. //查找com/jg/softbus/network/protocol/SessionAddr的java对象
  289. // jclass sessionAddrClass = (*env)->FindClass(env, "com/jg/softbus/network/protocol/SessionAddr");
  290. // if(sessionAddrClass == NULL){
  291. // printf("Cannot found the SessionAddr class.");
  292. // return NULL;
  293. // }
  294. //查找SessionAddr无参构造函数,函数结构为:SessionAddr()
  295. // jmethodID sessionAddrConstructor = (*env)->GetMethodID(env, sessionAddrClass, "<init>", "()V");
  296. // if(sessionAddrConstructor == NULL){
  297. // printf("Cannot found the constructor of SessionAddr.");
  298. // return NULL;
  299. // }
  300. //基于无参的构造函数创建ConnectionAddr对象
  301. jmethodID connectionAddrConstructor = (*env)->GetMethodID(env, connectionAddrClass, "<init>", "()V");
  302. jobject addrObj = (*env)->NewObject(env, connectionAddrClass, connectionAddrConstructor);
  303. //获取ConnectionAddrType对象
  304. jmethodID getForValueMethod = (*env)->GetStaticMethodID(env, connectionAddrTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/protocol/ConnectionAddrType;");
  305. if (getForValueMethod == NULL) {
  306. // 处理错误
  307. printf("Cannot found the method:getForValue(int)");
  308. return NULL;
  309. }
  310. //根据device提供的地址类型获取ConnectionAddrType对象
  311. jobject addrTypeObj = (*env)->CallStaticObjectMethod(env, connectionAddrTypeClass, getForValueMethod, addr->type);
  312. if(addrTypeObj == NULL) {
  313. // 处理错误
  314. printf("Cannot found the ConnectionAddrType. type info: %d", addr->type);
  315. return NULL;
  316. }
  317. //查找ConnectionAddr的setType(ConnectionAddrType type)方法
  318. jmethodID setTypeMethod = (*env)->GetMethodID(env, connectionAddrClass, "setType", "(Lcom/jg/softbus/network/protocol/ConnectionAddrType;)V");
  319. //设置地址类型到地址对象中
  320. (*env)->CallVoidMethod(env, addrObj, setTypeMethod, addrTypeObj);
  321. (*env)->DeleteLocalRef(env, addrTypeObj);
  322. //无参创建ConnectionAddrInfo的java对象
  323. jobject infoObj = (*env)->NewObject(env, connectionAddrInfoClass, connectionAddrInfoConstructor);
  324. //判断addr->type的类型,可参考下面代码的Switch方式
  325. switch (addr->type) {
  326. case CONNECTION_ADDR_WLAN: {
  327. printf("ip=%s,port=%d,", addr->info.ip.ip, addr->info.ip.port);
  328. }
  329. case CONNECTION_ADDR_ETH: {
  330. jstring ipstr = (*env)->NewStringUTF(env, addr->info.ip.ip);
  331. //并使用有参构造函数进行初始化
  332. jobject ipAddrObj = (*env)->NewObject(env, ipAddrClass, ipAddrConstructor, ipstr, addr->info.ip.port);
  333. //将地址对象写入ConnectionAddrInfo对象中
  334. jmethodID setIpMethod = (*env)->GetMethodID(env, connectionAddrClass, "setIp", "(Lcom/jg/softbus/network/protocol/IpAddr;)V");
  335. //设置地址类型到地址对象中
  336. (*env)->CallVoidMethod(env, addrObj, setIpMethod, ipAddrObj);
  337. (*env)->DeleteLocalRef(env, ipAddrObj);
  338. break;
  339. }
  340. case CONNECTION_ADDR_BR: {
  341. jstring brMacStr = (*env)->NewStringUTF(env, addr->info.br.brMac);
  342. //并使用有参构造函数进行初始化
  343. jobject brAddrObj = (*env)->NewObject(env, brAddrClass, brAddrConstructor, brMacStr);
  344. //将地址对象写入ConnectionAddrInfo对象中
  345. jmethodID setBrMethod = (*env)->GetMethodID(env, connectionAddrClass, "setBr", "(Lcom/jg/softbus/network/protocol/BrAddr;)V");
  346. //设置地址类型到地址对象中
  347. (*env)->CallVoidMethod(env, addrObj, setBrMethod, brAddrObj);
  348. (*env)->DeleteLocalRef(env, brAddrObj);
  349. (*env)->DeleteLocalRef(env, brMacStr);
  350. break;
  351. }
  352. case CONNECTION_ADDR_BLE: {
  353. //并使用构造函数进行初始化
  354. jobject bleAddrObj = (*env)->NewObject(env, bleAddrClass, bleAddrConstructor);
  355. // //获取BleProtocolType对象
  356. // jmethodID getForValueMethod2 = (*env)->GetStaticMethodID(env, bleProtocolTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/protocol/BleProtocolType;");
  357. // if (getForValueMethod2 == NULL) {
  358. // // 处理错误
  359. // printf("Cannot found the method:getForValue(int)");
  360. // return NULL;
  361. // }
  362. // //根据device提供的bleprotocol类型获取BleProtocolType对象
  363. // jobject bleProtocolTypeObj = (*env)->CallStaticObjectMethod(env, bleProtocolTypeClass, getForValueMethod2, addr->info.ble.protocol);
  364. // if(bleProtocolTypeObj == NULL) {
  365. // // 处理错误
  366. // printf("Cannot found the BleProtocolType. type info: %d", addr->info.ble.protocol);
  367. // return NULL;
  368. // }
  369. // //将地址对象写入ConnectionAddrInfo对象中
  370. // jmethodID setProtocolMethod = (*env)->GetMethodID(env, bleAddrClass, "setProtocol", "(Lcom/jg/softbus/network/protocol/BleProtocolType;)V");
  371. // (*env)->CallVoidMethod(env, bleAddrObj, setProtocolMethod, bleProtocolTypeObj);
  372. // (*env)->DeleteLocalRef(env, bleProtocolTypeObj);
  373. //setbleMac
  374. jmethodID setBleMacMethod = (*env)->GetMethodID(env, bleAddrClass, "setBleMac", "(Ljava/lang/String)V");
  375. jstring bleMac = (*env)->NewStringUTF(env, addr->info.ble.bleMac);
  376. (*env)->CallVoidMethod(env, bleAddrObj, setBleMacMethod, bleMac);
  377. (*env)->DeleteLocalRef(env, bleMac);
  378. //setUdidHash
  379. jmethodID setUdidHashMethod = (*env)->GetMethodID(env, bleAddrClass, "setUdidHash", "([B)V");
  380. size_t len = UDID_HASH_LEN / sizeof(uint8_t);
  381. jbyteArray byteArray = (*env)->NewByteArray(env, len);
  382. (*env)->SetByteArrayRegion(env, byteArray, 0, len, (const jbyte*)addr->info.ble.udidHash);
  383. // 将udidHash转换为整数
  384. // int hash_value = 0;
  385. // for (int k = 0; k < UDID_HASH_LEN; k++) {
  386. // hash_value = (hash_value << 8) + addr->info.ble.udidHash[k];
  387. // }
  388. (*env)->CallVoidMethod(env, bleAddrObj, setUdidHashMethod, byteArray);
  389. (*env)->DeleteLocalRef(env, byteArray);
  390. // //setPsm
  391. // jmethodID setPsmMethod = (*env)->GetMethodID(env, bleAddrClass, "setPsm", "(I)V");
  392. // (*env)->CallVoidMethod(env, bleAddrObj, setPsmMethod, addr->info.ble.psm);
  393. //将地址对象写入ConnectionAddrInfo对象中
  394. jmethodID setBleMethod = (*env)->GetMethodID(env, connectionAddrClass, "setBle", "(Lcom/jg/softbus/network/protocol/BrAddr;)V");
  395. (*env)->CallVoidMethod(env, addrObj, setBleMethod, bleAddrObj);
  396. (*env)->DeleteLocalRef(env, bleAddrObj);
  397. printf("bleMac=%s,", addr->info.ble.bleMac);
  398. break;
  399. }
  400. // case CONNECTION_ADDR_SESSION:
  401. // //创建SessionAddr对象
  402. // jobject sessionAddrObj = (*env)->NewObject(env, sessionAddrClass, sessionAddrConstructor);
  403. // //获取void setSessionId(Integer sessionId)方法
  404. // jmethodID setSessionIdMethod = (*env)->GetMethodID(env, sessionAddrClass, "setSessionId", "(I)V");
  405. // //将device中的sessionId调用方法设置到sessionAddrObj中
  406. // (*env)->CallVoidMethod(env, sessionAddrObj, setSessionIdMethod, addr->info.session.sessionId);
  407. // //获取void setChannelId(Integer channelId)方法
  408. // jmethodID setChannelIdMethod = (*env)->GetMethodID(env, sessionAddrClass, "setChannelId", "(I)V");
  409. // //将device中的channelId调用方法设置到sessionAddrObj中
  410. // (*env)->CallVoidMethod(env, sessionAddrObj, setChannelIdMethod, addr->info.session.channelId);
  411. // //获取void setType(String type)方法
  412. // jmethodID setTypeMethod = (*env)->GetMethodID(env, sessionAddrClass, "setType", "(Ljava/lang/String;)V");
  413. // //将device中的type调用方法设置到sessionAddrObj中
  414. // jstring sessionTypeStr = (*env)->NewStringUTF(env, addr->info.session.type);
  415. // (*env)->CallVoidMethod(env, sessionAddrObj, setTypeMethod, sessionTypeStr);
  416. // jmethodID setSessionMethod = (*env)->GetMethodID(env, connectionAddrClass, "setSession", "(Lcom/jg/softbus/network/protocol/SessionAddr;)V");
  417. // (*env)->CallVoidMethod(env, addrObj, setSessionMethod, sessionAddrObj);
  418. // (*env)->DeleteLocalRef(env, sessionTypeStr);
  419. // (*env)->DeleteLocalRef(env, sessionAddrObj);
  420. // printf("sessionId=%d, channelId=%d, type=%d", addr->info.session.sessionId, addr->info.session.channelId, addr->info.session.type);
  421. // break;
  422. default:
  423. break;
  424. }
  425. //获取ConnectionAddr中的void setPeerUid(String peerUid)方法,将device中的peerUid放入到addrObj中
  426. jmethodID setPeerUidMethod = (*env)->GetMethodID(env, connectionAddrClass, "setPeerUid", "(Ljava/lang/String;)V");
  427. jstring peerUidStr = (*env)->NewStringUTF(env, addr->peerUid);
  428. (*env)->CallVoidMethod(env, addrObj, setPeerUidMethod, peerUidStr);
  429. (*env)->DeleteLocalRef(env, peerUidStr);
  430. return addrObj;
  431. }
  432. void onJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t retCode)
  433. {
  434. // 获取JNIEnv环境
  435. JNIEnv *env;
  436. // 将当前线程附加到Java虚拟机
  437. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  438. // 获取当前对象的类引用
  439. jclass nativeLibClass = (*env)->GetObjectClass(env, lNNCallback);
  440. // 获取当前对象的publishSuccess方法的ID
  441. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onJoinLNNResult", "(Lcom/jg/softbus/network/protocol/ConnectionAddr;Ljava/lang/String;I)V");
  442. //将ConnectionAddr转换为java对应的对象
  443. jobject addrObj = parseAddrToJava(env, addr);
  444. if(addrObj == NULL) {
  445. return;
  446. }
  447. jstring networkIdStr = (*env)->NewStringUTF(env, networkId);
  448. printf("callback the join event. networkId:%s", networkId);
  449. //调用回调方法执行
  450. (*env)->CallVoidMethod(env, lNNCallback, callbackMethod, addrObj, networkIdStr, retCode);
  451. printf("callback the join event completed. networkId:%s", networkId);
  452. //删除本地对象
  453. (*env)->DeleteLocalRef(env, addrObj);
  454. //将当前线程从java虚拟机中移除
  455. (*jvm)->DetachCurrentThread(jvm);
  456. }
  457. /*
  458. * Class: com_jg_softbus_naservice_LNNService
  459. * Method: joinLNN
  460. * Signature: (Ljava/lang/String;Lcom/jg/softbus/network/protocol/ConnectionAddr;Lcom/jg/softbus/network/callback/ILNNCallback;)I
  461. */
  462. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_LNNService_joinLNN
  463. (JNIEnv *env, jobject obj, jstring pkgName, jobject targetAddr, jobject callback) {
  464. // 保存Java虚拟机的引用
  465. (*env)->GetJavaVM(env, &jvm);
  466. lNNCallback = (*env)->NewGlobalRef(env, callback);
  467. //包名处理
  468. const char *pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  469. ConnectionAddr addr;
  470. int ret = parseConnectAddr(env, targetAddr, &addr);
  471. if(ret !=0) {
  472. // 尝试进行一些可能会抛出异常的操作
  473. jclass exceptionClass = (*env)->FindClass(env, "java/lang/Exception"); // 获取异常类
  474. // 如果找不到异常类,则抛出异常
  475. jmethodID exceptionConstructor = (*env)->GetMethodID(env, exceptionClass, "<init>", "()V");
  476. jthrowable exception = (*env)->NewObject(env, exceptionClass, exceptionConstructor);
  477. // 抛出异常
  478. (*env)->ThrowNew(env, exceptionClass, "ConnectionAddr object error.");
  479. //解析失败无法继续操作
  480. return -1;
  481. }
  482. printf("joinLNN start...");
  483. ret = JoinLNN(pkgNameStr, &addr, onJoinLNNResult);
  484. printf("joinLNN completed...");
  485. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  486. return ret;
  487. }
  488. /**
  489. * 离开网络回调函数
  490. */
  491. void onLeaveLNNResult(const char *networkId, int32_t retCode)
  492. {
  493. // 获取JNIEnv环境
  494. JNIEnv *env;
  495. // 将当前线程附加到Java虚拟机
  496. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  497. // 获取当前对象的类引用
  498. jclass nativeLibClass = (*env)->GetObjectClass(env, leaveCallback);
  499. // 获取当前对象的publishSuccess方法的ID
  500. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onLeaveLNNResult", "(Ljava/lang/String;I)V");
  501. jstring networkIdStr = (*env)->NewStringUTF(env, networkId);
  502. printf("callback the leave event. networkId:%s", networkId);
  503. //调用回调方法执行
  504. (*env)->CallVoidMethod(env, lNNCallback, callbackMethod, networkIdStr, retCode);
  505. printf("callback the leave event completed. networkId:%s", networkId);
  506. //将当前线程从java虚拟机中移除
  507. (*jvm)->DetachCurrentThread(jvm);
  508. }
  509. /*
  510. * Class: com_jg_softbus_naservice_LNNService
  511. * Method: leaveLNN
  512. * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/jg/softbus/network/callback/ILNNCallback;)I
  513. */
  514. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_LNNService_leaveLNN
  515. (JNIEnv *env, jobject obj, jstring pkgName, jstring networkId, jobject callback) {
  516. // 保存Java虚拟机的引用
  517. (*env)->GetJavaVM(env, &jvm);
  518. leaveCallback = (*env)->NewGlobalRef(env, callback);
  519. //包名处理
  520. const char *pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  521. const char *networkIdStr = (*env)->GetStringUTFChars(env, networkId, NULL);
  522. printf("leave start.");
  523. int ret = LeaveLNN(pkgNameStr, networkIdStr, onLeaveLNNResult);
  524. printf("leave completed.");
  525. (*env)->ReleaseStringUTFChars(env, networkId, networkIdStr);
  526. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  527. return ret;
  528. }
  529. /**
  530. * 转换NodeBasicInfo对象为java的对象
  531. */
  532. jobject parseNodeBasicInfoToJava(JNIEnv *env, NodeBasicInfo *info) {
  533. //创建java的DeviceInfo对象
  534. jclass nodeBasicInfoClass = (*env)->FindClass(env, "com/jg/softbus/network/node/NodeBasicInfo");
  535. if (nodeBasicInfoClass == NULL) {
  536. // 处理错误
  537. printf("Cannot found the java class:com/jg/softbus/network/node/NodeBasicInfo");
  538. return NULL;
  539. }
  540. jmethodID constructor = (*env)->GetMethodID(env, nodeBasicInfoClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;S)V");
  541. if (constructor == NULL) {
  542. // 处理错误
  543. printf("Cannot found the default constructor of NodeBasicInfo.");
  544. return NULL;
  545. }
  546. jstring networkId = (*env)->NewStringUTF(env, info->networkId);
  547. jstring deviceName = (*env)->NewStringUTF(env, info->deviceName);
  548. jobject nbInfoObj = (*env)->NewObject(env, nodeBasicInfoClass, constructor, networkId, deviceName, info->deviceTypeId);
  549. return nbInfoObj;
  550. }
  551. /**
  552. * 节点上线事件回调
  553. */
  554. void onNodeOnline(NodeBasicInfo *info) {
  555. // 获取JNIEnv环境
  556. JNIEnv *env;
  557. // 将当前线程附加到Java虚拟机
  558. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  559. // 获取当前对象的类引用
  560. jclass nativeLibClass = (*env)->GetObjectClass(env, nodeStateCb);
  561. // 获取当前对象的publishSuccess方法的ID
  562. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onNodeOnline", "(Lcom/jg/softbus/network/node/NodeBasicInfo;)V");
  563. //将NodeBasicInfo转换为java对应的对象
  564. jobject nodeBasicInfoObj = parseNodeBasicInfoToJava(env, info);
  565. if(nodeBasicInfoObj == NULL) {
  566. return;
  567. }
  568. printf("listen the node online.");
  569. //调用回调方法执行
  570. (*env)->CallVoidMethod(env, nodeStateCb, callbackMethod, nodeBasicInfoObj);
  571. printf("java listen node online completed.");
  572. //删除本地对象
  573. (*env)->DeleteLocalRef(env, nodeBasicInfoObj);
  574. //将当前线程从java虚拟机中移除
  575. (*jvm)->DetachCurrentThread(jvm);
  576. }
  577. /**
  578. * 节点下线事件回调
  579. */
  580. void onNodeOffline(NodeBasicInfo *info) {
  581. // 获取JNIEnv环境
  582. JNIEnv *env;
  583. // 将当前线程附加到Java虚拟机
  584. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  585. // 获取当前对象的类引用
  586. jclass nativeLibClass = (*env)->GetObjectClass(env, nodeStateCb);
  587. // 获取当前对象的publishSuccess方法的ID
  588. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onNodeOffline", "(Lcom/jg/softbus/network/node/NodeBasicInfo;)V");
  589. //将NodeBasicInfo转换为java对应的对象
  590. jobject nodeBasicInfoObj = parseNodeBasicInfoToJava(env, info);
  591. if(nodeBasicInfoObj == NULL) {
  592. return;
  593. }
  594. printf("listen the node offline.");
  595. //调用回调方法执行
  596. (*env)->CallVoidMethod(env, nodeStateCb, callbackMethod, nodeBasicInfoObj);
  597. printf("java listen node offline completed.");
  598. //删除本地对象
  599. (*env)->DeleteLocalRef(env, nodeBasicInfoObj);
  600. //将当前线程从java虚拟机中移除
  601. (*jvm)->DetachCurrentThread(jvm);
  602. }
  603. /**
  604. * 节点信息变化事件回调
  605. */
  606. void onNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info) {
  607. // 获取JNIEnv环境
  608. JNIEnv *env;
  609. // 将当前线程附加到Java虚拟机
  610. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  611. // 获取当前对象的类引用
  612. jclass nativeLibClass = (*env)->GetObjectClass(env, nodeStateCb);
  613. // 获取当前对象的publishSuccess方法的ID
  614. jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onNodeBasicInfoChanged", "(Lcom/jg/softbus/network/node/NodeBasicInfoType;Lcom/jg/softbus/network/node/NodeBasicInfo;)V");
  615. //将NodeBasicInfo转换为java对应的对象
  616. jobject nodeBasicInfoObj = parseNodeBasicInfoToJava(env, info);
  617. if(nodeBasicInfoObj == NULL) {
  618. return;
  619. }
  620. //创建java的NodeBasicInfoType对象
  621. jclass nodeBasicInfoTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/node/NodeBasicInfoType");
  622. if (nodeBasicInfoTypeClass == NULL) {
  623. // 处理错误
  624. printf("Cannot found the java class:com/jg/softbus/network/node/NodeBasicInfoType");
  625. return;
  626. }
  627. jmethodID getForValueMethod = (*env)->GetStaticMethodID(env, nodeBasicInfoTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/node/NodeBasicInfoType;");
  628. if (getForValueMethod == NULL) {
  629. // 处理错误
  630. printf("Cannot found the static method:getForValue(int)");
  631. return;
  632. }
  633. jobject nodeBasicInfoTypeObj = (*env)->CallStaticObjectMethod(env, nodeBasicInfoTypeClass, getForValueMethod, (int)type);
  634. if (nodeBasicInfoTypeObj == NULL) {
  635. // 处理错误
  636. printf("Cannot found NodeBasicInfoType. the type is: %d", type);
  637. return;
  638. }
  639. printf("listen the node change.");
  640. //调用回调方法执行
  641. (*env)->CallVoidMethod(env, nodeStateCb, callbackMethod, nodeBasicInfoTypeObj, nodeBasicInfoObj);
  642. printf("java listen node chagned completed.");
  643. //删除本地对象
  644. (*env)->DeleteLocalRef(env, nodeBasicInfoObj);
  645. //删除本地对象
  646. (*env)->DeleteLocalRef(env, nodeBasicInfoTypeObj);
  647. //将当前线程从java虚拟机中移除
  648. (*jvm)->DetachCurrentThread(jvm);
  649. }
  650. /**
  651. * 将c的NodeStatus结构转换为java对应的对象
  652. */
  653. // jobject parseNodeStatusToJava(JNIEnv *env, NodeStatus *status) {
  654. // //创建java的NodeStatusType对象
  655. // jclass nodeStatusClass = (*env)->FindClass(env, "com/jg/softbus/network/node/NodeStatus");
  656. // if(nodeStatusClass == NULL) {
  657. // printf("Cannot found the java class:com/jg/softbus/network/node/NodeStatus");
  658. // return NULL;
  659. // }
  660. // jmethodID constructor = (*env)->GetMethodID(env, nodeStatusClass, "<init>", "()V");
  661. // if (constructor == NULL) {
  662. // // 处理错误
  663. // printf("Cannot found the default constructor of NodeStatus.");
  664. // return NULL;
  665. // }
  666. // jobject obj = (*env)->NewObject(env, nodeStatusClass, constructor);
  667. // if (obj == NULL) {
  668. // // 处理错误
  669. // printf("Cannot create the NodeStatus object.");
  670. // return NULL;
  671. // }
  672. // //设置basicInfo
  673. // jmethodID setBasicInfoMethod = (*env)->GetMethodID(env, nodeStatusClass, "setBasicInfo", "(Lcom/jg/softbus/network/node/NodeBasicInfo;)V");
  674. // jobject nodeBasicInfoObj = parseNodeBasicInfoToJava(env, status->basicInfo);
  675. // (*env)->CallVoidMethod(env, obj, setBasicInfoMethod, nodeBasicInfoObj);
  676. // (*env)->DeleteLocalRef(env, nodeBasicInfoObj);
  677. // //authStatus
  678. // jmethodID setAuthStatusMethod = (*env)->GetMethodID(env, nodeStatusClass, "setAuthStatus", "(S)V");
  679. // (*env)->CallVoidMethod(env, obj, setAuthStatusMethod, status->authStatus);
  680. // //dataBaseStatus
  681. // jmethodID setDataBaseStatusMethod = (*env)->GetMethodID(env, nodeStatusClass, "setDataBaseStatus", "(S)V");
  682. // (*env)->CallVoidMethod(env, obj, setDataBaseStatusMethod, status->dataBaseStatus);
  683. // //meshType
  684. // jmethodID setMeshTypeMethod = (*env)->GetMethodID(env, nodeStatusClass, "setMeshType", "(S)V");
  685. // (*env)->CallVoidMethod(env, obj, setMeshTypeMethod, status->meshType);
  686. // //reserved
  687. // jmethodID setReservedMethod = (*env)->GetMethodID(env, nodeStatusClass, "setReserved", "([S)V");
  688. // (*env)->CallVoidMethod(env, obj, setReservedMethod, (jshort*)status->meshType);
  689. // reutrn obj;
  690. // }
  691. /**
  692. * 设备运行状态变化事件回调
  693. */
  694. // void onNodeStatusChanged(NodeStatusType type, NodeStatus *status) {
  695. // // 获取JNIEnv环境
  696. // JNIEnv *env;
  697. // // 将当前线程附加到Java虚拟机
  698. // (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  699. // // 获取当前对象的类引用
  700. // jclass nativeLibClass = (*env)->GetObjectClass(env, nodeStateCb);
  701. // // 获取当前对象的publishSuccess方法的ID
  702. // jmethodID callbackMethod = (*env)->GetMethodID(env, nativeLibClass, "onNodeStatusChanged", "(Lcom/jg/softbus/network/node/NodeStatusType;Lcom/jg/softbus/network/node/status;)V");
  703. // //将NodeBasicInfo转换为java对应的对象
  704. // jobject nodeStatusObj = parseNodeStatusToJava(status);
  705. // if(nodeStatusObj == NULL) {
  706. // return;
  707. // }
  708. // //创建java的NodeStatusType对象
  709. // jclass nodeStatusTypeClass = (*env)->FindClass(env, "com/jg/softbus/network/node/NodeStatusType");
  710. // if (nodeStatusTypeClass == NULL) {
  711. // // 处理错误
  712. // printf("Cannot found the java class:com/jg/softbus/network/node/NodeStatusType");
  713. // return;
  714. // }
  715. // jmethodID getForValueMethod = (*env)->GetStaticMethodID(env, nodeBasicInfoTypeClass, "getForValue", "(I)Lcom/jg/softbus/network/node/nodeStatusTypeClass;");
  716. // if (getForValueMethod == NULL) {
  717. // // 处理错误
  718. // printf("Cannot found the static method:getForValue(int)");
  719. // return;
  720. // }
  721. // jobject nodeStatusTypeObj = (*env)->CallStaticObjectMethod(env, nodeStatusTypeClass, getForValueMethod, (int)type);
  722. // if (nodeStatusTypeObj == NULL) {
  723. // // 处理错误
  724. // printf("Cannot found NodeStatusType. the type is: %d", type);
  725. // return;
  726. // }
  727. // printf("listen the node status change.");
  728. // //调用回调方法执行
  729. // (*env)->CallVoidMethod(env, nodeStateCb, callbackMethod, nodeStatusTypeObj, nodeStatusObj);
  730. // printf("java listen node status chagned completed.");
  731. // //删除本地对象
  732. // (*env)->DeleteLocalRef(env, nodeStatusObj);
  733. // //删除本地对象
  734. // (*env)->DeleteLocalRef(env, nodeStatusTypeObj);
  735. // //将当前线程从java虚拟机中移除
  736. // (*jvm)->DetachCurrentThread(jvm);
  737. // }
  738. /**
  739. * 创建全局回调对象
  740. */
  741. void createGlobalStateCb(JNIEnv *env, jobject nodeStateCallback)
  742. {
  743. //获取当前对象的类引用
  744. jclass nscbClass = (*env)->GetObjectClass(env, nodeStateCallback);
  745. // 获取当前对象的getEvents方法的ID
  746. jmethodID getEventsMethod = (*env)->GetMethodID(env, nscbClass, "getEvents", "()I");
  747. jint events = (*env)->CallIntMethod(env, nodeStateCallback, getEventsMethod);
  748. //监听有关所有事件
  749. g_stateCb.events = events;
  750. g_stateCb.onNodeOffline = onNodeOffline;
  751. g_stateCb.onNodeOnline = onNodeOnline;
  752. g_stateCb.onNodeBasicInfoChanged = onNodeBasicInfoChanged;
  753. // g_stateCb.onNodeStatusChanged = onNodeStatusChanged;
  754. }
  755. /*
  756. * Class: com_jg_softbus_naservice_LNNService
  757. * Method: regNodeDeviceStateCb
  758. * Signature: (Ljava/lang/String;Lcom/jg/softbus/network/callback/INodeStateCb;)I
  759. */
  760. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_LNNService_regNodeDeviceStateCb
  761. (JNIEnv *env, jobject obj, jstring pkgName, jobject nodeStateCallback) {
  762. // 保存Java虚拟机的引用
  763. (*env)->GetJavaVM(env, &jvm);
  764. nodeStateCb = (*env)->NewGlobalRef(env, nodeStateCallback);
  765. //包名处理
  766. const char *pkgNameStr = (*env)->GetStringUTFChars(env, pkgName, NULL);
  767. createGlobalStateCb(env, nodeStateCallback);
  768. int ret = RegNodeDeviceStateCb(pkgNameStr, &g_stateCb);
  769. (*env)->ReleaseStringUTFChars(env, pkgName, pkgNameStr);
  770. return ret;
  771. }
  772. /*
  773. * Class: com_jg_softbus_naservice_LNNService
  774. * Method: unRegNodeDeviceStateCb
  775. * Signature: (Lcom/jg/softbus/network/callback/INodeStateCb;)I
  776. */
  777. JNIEXPORT jint JNICALL Java_com_jg_softbus_naservice_LNNService_unRegNodeDeviceStateCb
  778. (JNIEnv *env, jobject obj, jobject nodeStateCallback) {
  779. // 保存Java虚拟机的引用
  780. (*env)->GetJavaVM(env, &jvm);
  781. int ret = 0x00;
  782. if (&g_stateCb != NULL) {
  783. //注销事件
  784. ret = UnregNodeDeviceStateCb(&g_stateCb);
  785. }
  786. return ret;
  787. }
  788. /*
  789. * Class: com_jg_softbus_naservice_LNNService
  790. * Method: destroy
  791. * Signature: ()V
  792. */
  793. JNIEXPORT void JNICALL Java_com_jg_softbus_naservice_LNNService_destroy
  794. (JNIEnv *env, jobject obj) {
  795. // 删除全局引用
  796. (*env)->DeleteGlobalRef(env, lNNCallback);
  797. (*env)->DeleteGlobalRef(env, leaveCallback);
  798. (*env)->DeleteGlobalRef(env, nodeStateCb);
  799. free(jvm);
  800. }