AutoLink.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "AutoLink.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #ifdef USE_AUTO
  6. IUserAutoImpl::IUserAutoImpl(AutoLink* handle) : mHandle(handle)/*, mRecBuf(NULL)*/, mRecStart(false),
  7. mUsedPos(0)
  8. {
  9. }
  10. IUserAutoImpl::~IUserAutoImpl()
  11. {
  12. }
  13. void IUserAutoImpl::videoStart(int width, int height, int offsetX, int offsetY)
  14. {
  15. if(mHandle){
  16. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  17. printf("auto video width = %d, height = %d, offsetX = %d, offsetY =%d\r\n", width, height, offsetX, offsetY);
  18. mHandle->video_start(offsetX, offsetY,width - offsetX * 2, height - offsetY *2);
  19. mHandle->onSdkConnectStatus(CONNECT_STATUS_CONNECT_SUCCEED, mHandle->getPhoneType());
  20. mHandle->mConnectStatus = CONNECT_STATUS_CONNECT_SUCCEED;
  21. }
  22. }
  23. void IUserAutoImpl::videoStop()
  24. {
  25. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  26. if(mHandle)
  27. mHandle->video_stop();
  28. }
  29. void IUserAutoImpl::videoPlay(char *buf, int len)
  30. {
  31. if(mHandle)
  32. mHandle->video_play(buf, len);
  33. }
  34. void IUserAutoImpl::audioStart(int type, int rate, int channels, int bits)
  35. {
  36. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  37. if(mHandle){
  38. mHandle->audio_start(ChangeAudioType((AutoAudioStreamType)type), rate, bits, channels);
  39. }
  40. }
  41. void IUserAutoImpl::audioStop(int type)
  42. {
  43. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  44. if(mHandle){
  45. mHandle->audio_stop(ChangeAudioType((AutoAudioStreamType)type));
  46. }
  47. }
  48. void IUserAutoImpl::audioPlay(int type, char *buf, int len)
  49. {
  50. //printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  51. if(mHandle){
  52. mHandle->audio_play(ChangeAudioType((AutoAudioStreamType)type), buf, len);
  53. }
  54. }
  55. void IUserAutoImpl::recordStart(int rate, int channels, int bits)
  56. {
  57. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  58. if(mHandle){
  59. mHandle->record_pause(false);
  60. mHandle->app_status(APP_RECOGNITION_STARTED);
  61. }
  62. }
  63. void IUserAutoImpl::recordStop()
  64. {
  65. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  66. if(mHandle){
  67. mHandle->record_pause(true);
  68. mHandle->app_status(APP_RECOGNITION_STOPPED);
  69. }
  70. }
  71. void IUserAutoImpl::recordProc(char *buf, int len)
  72. {
  73. strncpy(buf , mHandle->getRecData().c_str(), len);
  74. mHandle->getRecData().clear();
  75. }
  76. void IUserAutoImpl::notifyStatus(int state)
  77. {
  78. printf("11 notify status = %d\r\n", state);
  79. static bool running = false;
  80. if(state == LINK_REMOVED || state == LINK_DISCONNECTED || state == LINK_FAIL){
  81. running = false;
  82. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  83. mHandle->record_stop();
  84. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  85. mHandle->onSdkConnectStatus(CONNECT_STATUS_DISCONNECTED, UnKnown);
  86. mHandle->mConnectStatus = CONNECT_STATUS_DISCONNECTED;
  87. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  88. }
  89. else if(state == LINK_SUCCESS){
  90. if(!running){
  91. AudioInfo info = {16000, 1, 16};
  92. mHandle->record_start(info);
  93. mHandle->record_pause(true);
  94. mHandle->onSdkConnectStatus(CONNECT_STATUS_CONNECT_SUCCEED, mHandle->getPhoneType());
  95. mHandle->mConnectStatus = CONNECT_STATUS_CONNECT_SUCCEED;
  96. running = true;
  97. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  98. }
  99. }
  100. else if(state == LINK_EXITING){
  101. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  102. mHandle->SetBackground();
  103. mHandle->app_status(APP_BACKGROUND, nullptr);
  104. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  105. }
  106. else if(state == LINK_STARTING){
  107. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  108. //mHandle->SetForeground();
  109. //mHandle->app_status(APP_FOREGROUND, nullptr);
  110. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  111. }
  112. printf("22 notify status = %d\r\n", state);
  113. }
  114. void IUserAutoImpl::notifyPhoneBtInfo(const char *phoneBTAddr, int pairMethod)
  115. {
  116. }
  117. void IUserAutoImpl::getLocalBtAddr(char* mac)
  118. {
  119. string data = mHandle->getCarBtAddress();
  120. mac = (char*)data.c_str();
  121. }
  122. AudioType IUserAutoImpl::ChangeAudioType(AutoAudioStreamType type)
  123. {
  124. AudioType ret = AUDIO_TYPE_MUSIC;
  125. if(type == AUTO_AUDIO_STREAM_MEDIA){
  126. ret = AUDIO_TYPE_MUSIC;
  127. }
  128. else if(type == AUTO_AUDIO_STREAM_GUIDANCE){
  129. ret = AUDIO_TYPE_VR;
  130. }
  131. else if(type == AUTO_AUDIO_STREAM_SYSTEM_AUDIO){
  132. ret = AUDIO_TYPE_TTS;
  133. }
  134. else if(type == AUTO_AUDIO_STREAM_TELEPHONY){
  135. ret = AUDIO_TYPE_CALL;
  136. }
  137. return ret;
  138. }
  139. #endif
  140. AutoLink::AutoLink()
  141. {
  142. #ifdef USE_AUTO
  143. mHandle = new AndroidAuto();
  144. mAutoCbs = new IUserAutoImpl(this);
  145. mHandle->registerCallbacks(mAutoCbs);
  146. mConnectStatus = CONNECT_STATUS_DISCONNECTED;
  147. mDefaultWifi = true;
  148. #endif
  149. }
  150. AutoLink::~AutoLink()
  151. {
  152. #ifdef USE_AUTO
  153. delete mHandle;
  154. delete mAutoCbs;
  155. #endif
  156. }
  157. #ifdef USE_AUTO
  158. bool AutoLink::init(LinkMode linkMode)
  159. {
  160. if(mHandle){
  161. printf("auto w:%d,h%d\r\n",mLinkConfig.screen_width,mLinkConfig.screen_height);
  162. mLinkMode = linkMode;
  163. mHandle->setConfig("video_width",mLinkConfig.screen_width);
  164. mHandle->setConfig("video_height",mLinkConfig.screen_height);
  165. mHandle->setConfig("video_density",160);
  166. if(!(mLinkConfig.car_wifi_ssid.empty()| mLinkConfig.car_wifi_passphrase.empty() | mLinkConfig.car_wifi_channel.empty())){
  167. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  168. if(mDefaultWifi){
  169. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  170. mDefaultWifi = true;
  171. mHandle->setConfig("WIFI_SSID", mLinkConfig.car_wifi_ssid.c_str());
  172. mHandle->setConfig("WIFI_PASSWD",mLinkConfig.car_wifi_passphrase.c_str());
  173. mHandle->setConfig("WIFI_CHANNEL",stoi(mLinkConfig.car_wifi_channel.c_str()));
  174. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  175. }
  176. }
  177. }
  178. return true;
  179. }
  180. bool AutoLink::start()
  181. {
  182. if(mHandle){
  183. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  184. if(mConnectStatus == CONNECT_STATUS_DISCONNECTED){
  185. onSdkConnectStatus(CONNECT_STATUS_CONNECTING, mPhoneType);
  186. if(mLinkMode == Wired)
  187. mHandle->startSession(false);
  188. else
  189. mHandle->startSession(true);
  190. }
  191. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  192. }
  193. return true;
  194. }
  195. bool AutoLink::stop()
  196. {
  197. if(mHandle){
  198. mHandle->getVideoFocus();
  199. mHandle->releaseAudioFocus();
  200. }
  201. return true;
  202. }
  203. bool AutoLink::release()
  204. {
  205. if(mHandle){
  206. mHandle->getVideoFocus();
  207. mHandle->getAudioFocus();
  208. }
  209. return true;
  210. }
  211. bool AutoLink::start_mirror()
  212. {
  213. if(mHandle)
  214. mHandle->releaseVideoFocus();
  215. return true;
  216. }
  217. bool AutoLink::stop_mirror()
  218. {
  219. if(mHandle)
  220. mHandle->getVideoFocus();
  221. return true;
  222. }
  223. bool AutoLink::set_background()
  224. {
  225. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  226. if(mHandle)
  227. mHandle->getVideoFocus();
  228. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  229. return true;
  230. }
  231. bool AutoLink::set_foreground()
  232. {
  233. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  234. if(mHandle)
  235. mHandle->releaseVideoFocus();
  236. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  237. return true;
  238. }
  239. bool AutoLink::get_audio_focus()
  240. {
  241. if(mHandle)
  242. mHandle->getAudioFocus();
  243. return true;
  244. }
  245. bool AutoLink::release_audio_focus()
  246. {
  247. if(mHandle)
  248. mHandle->releaseAudioFocus();
  249. return true;
  250. }
  251. void AutoLink::set_inserted(bool inserted, PhoneType phoneType)
  252. {
  253. mPhoneType = phoneType;
  254. }
  255. void AutoLink::send_screen_size(int width, int height)
  256. {
  257. mLinkConfig.screen_width = width;
  258. mLinkConfig.screen_height = height;
  259. }
  260. void AutoLink::record_audio_callback(unsigned char *data, int len)
  261. {
  262. std::lock_guard<std::mutex> lock(mMutex);
  263. if(len > 0)
  264. mRecData += string((char*)data, len);
  265. }
  266. void AutoLink::send_car_bluetooth(const string& name, const string& address, const string& pin)
  267. {
  268. mbtaddress = address;
  269. }
  270. void AutoLink::send_phone_bluetooth(const string& address)
  271. {
  272. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  273. }
  274. void AutoLink::send_car_wifi(WifiInfo& info){
  275. if(mHandle){
  276. mDefaultWifi = false;
  277. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  278. mHandle->setConfig("WIFI_SSID", info.ssid.c_str());
  279. mHandle->setConfig("WIFI_PASSWD",info.passphrase.c_str());
  280. mHandle->setConfig("WIFI_CHANNEL",stoi(info.channel_id.c_str()));
  281. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  282. }
  283. }
  284. void AutoLink::send_touch(int x, int y, TouchCode touchCode)
  285. {
  286. printf("auto x:%d, y:%d, press:%d\r\n",x, y, touchCode);
  287. if(mHandle){
  288. if(touchCode == Touch_Up)
  289. mHandle->sendTouchEvent(x, y, 1);
  290. else if(touchCode == Touch_Press)
  291. mHandle->sendTouchEvent(x, y, 0);
  292. else if(touchCode == Touch_Move)
  293. mHandle->sendTouchEvent(x, y, 2);
  294. }
  295. }
  296. bool AutoLink::send_key(KeyCode keyCode)
  297. {
  298. AutoKeyCode ret;
  299. switch(keyCode) {
  300. case KEY_HOME:
  301. ret = KEYCODE_HOME;
  302. break;
  303. case KEY_SYSTEM_HOME:
  304. ret = KEYCODE_MENU;
  305. break;
  306. case KEY_CAR_BACK:
  307. ret = KEYCODE_BACK;
  308. break;
  309. case KEY_SYSTEM_BACK:
  310. ret = KEYCODE_BACK;
  311. break;
  312. case KEY_VOICE_ASSISTANT:
  313. ret = KEYCODE_SEARCH;
  314. break;
  315. case KEY_MUSIC_PLAY_PAUSE:
  316. ret = KEYCODE_MEDIA_PLAY_PAUSE;
  317. break;
  318. case KEY_MUSIC_PLAY:
  319. ret = KEYCODE_MEDIA_PLAY;
  320. break;
  321. case KEY_MUSIC_PAUSE:
  322. ret = KEYCODE_MEDIA_STOP;
  323. break;
  324. case KEY_MUSIC_NEXT:
  325. ret = KEYCODE_MEDIA_NEXT;
  326. break;
  327. case KEY_MUSIC_PREVIOUS:
  328. ret = KEYCODE_MEDIA_PREVIOUS;
  329. break;
  330. case KEY_PHONE:
  331. {
  332. static bool call = false;
  333. if(!call)
  334. {
  335. ret = KEYCODE_CALL;
  336. }else
  337. {
  338. ret = KEYCODE_ENDCALL;
  339. }
  340. call = !call;
  341. }
  342. break;
  343. case KEY_LIGHTMODE:
  344. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  345. mHandle->setConfig("night_mode", 0);
  346. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  347. return true;
  348. case KEY_NIGHTMODE:
  349. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  350. mHandle->setConfig("night_mode", 1);
  351. return true;
  352. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  353. default:
  354. break;
  355. }
  356. if(ret == KEY_CAR_BACKGROUND){
  357. set_background();
  358. }
  359. else if(ret == KEY_CAR_FOREGROUND){
  360. set_foreground();
  361. }
  362. else
  363. {
  364. mHandle->sendKeyEvent(ret, true);
  365. usleep(20000);
  366. mHandle->sendKeyEvent(ret, false);
  367. }
  368. }
  369. bool AutoLink::send_wheel(WheelCode wheel, bool foucs)
  370. {
  371. if (!foucs) {
  372. mHandle->sendKeyEvent(KEYCODE_DPAD_CENTER, true);
  373. usleep(20000);
  374. mHandle->sendKeyEvent(KEYCODE_DPAD_CENTER, false);
  375. } else {
  376. mHandle->sendKnobEvent(KEYCODE_ROTARY_CONTROLLER, wheel);
  377. }
  378. }
  379. bool AutoLink::send_night_mode(bool night)
  380. {
  381. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  382. if(mHandle){
  383. mHandle->setConfig("night_mode", night);
  384. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  385. }
  386. return true;
  387. }
  388. bool AutoLink::send_right_hand_driver(bool right)
  389. {
  390. if(mHandle)
  391. mHandle->setConfig("driver_position", right);
  392. return true;
  393. }
  394. bool AutoLink::open_page(AppPage appPage)
  395. {
  396. int ret = 0;
  397. if(appPage == APP_PAGE_NAVIGATION){
  398. ret = KEYCODE_NAVIGATION;
  399. }
  400. else if(appPage == APP_PAGE_MAIN){
  401. ret = KEYCODE_HOME;
  402. }
  403. else if(appPage == APP_PAGE_MUSIC){
  404. ret = KEYCODE_MUSIC;
  405. }
  406. mHandle->sendKeyEvent(ret, true);
  407. usleep(20000);
  408. mHandle->sendKeyEvent(ret, false);
  409. }
  410. void AutoLink::request_status(RequestAppStatus requestAppStatus, void *reserved)
  411. {
  412. }
  413. void AutoLink::setRecData(string str)
  414. {
  415. mRecData = str;
  416. }
  417. #endif