SoftbusLNN.c 40 KB

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