AutoLink.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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),mConnectStatus(CONNECT_STATUS_DISCONNECTED)
  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. 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){
  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. 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. 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. #endif
  146. }
  147. AutoLink::~AutoLink()
  148. {
  149. #ifdef USE_AUTO
  150. delete mHandle;
  151. delete mAutoCbs;
  152. #endif
  153. }
  154. #ifdef USE_AUTO
  155. bool AutoLink::init(LinkMode linkMode)
  156. {
  157. if(mHandle){
  158. printf("auto w:%d,h%d\r\n",mLinkConfig.screen_width,mLinkConfig.screen_height);
  159. mLinkMode = linkMode;
  160. mHandle->registerCallbacks(mAutoCbs);
  161. mHandle->setConfig("video_width",mLinkConfig.screen_width);
  162. mHandle->setConfig("video_height",mLinkConfig.screen_height);
  163. mHandle->setConfig("video_density",160);
  164. }
  165. return true;
  166. }
  167. bool AutoLink::start()
  168. {
  169. if(mHandle){
  170. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  171. onSdkConnectStatus(CONNECT_STATUS_CONNECTING, mPhoneType);
  172. if(mLinkMode == Wired)
  173. mHandle->startSession(false);
  174. else
  175. mHandle->startSession(true);
  176. }
  177. return true;
  178. }
  179. bool AutoLink::stop()
  180. {
  181. if(mHandle){
  182. mHandle->getVideoFocus();
  183. mHandle->releaseAudioFocus();
  184. }
  185. return true;
  186. }
  187. bool AutoLink::release()
  188. {
  189. if(mHandle){
  190. mHandle->getVideoFocus();
  191. mHandle->getAudioFocus();
  192. }
  193. return true;
  194. }
  195. bool AutoLink::start_mirror()
  196. {
  197. if(mHandle)
  198. mHandle->releaseVideoFocus();
  199. return true;
  200. }
  201. bool AutoLink::stop_mirror()
  202. {
  203. if(mHandle)
  204. mHandle->getVideoFocus();
  205. return true;
  206. }
  207. bool AutoLink::set_background()
  208. {
  209. if(mHandle)
  210. mHandle->getVideoFocus();
  211. return true;
  212. }
  213. bool AutoLink::set_foreground()
  214. {
  215. if(mHandle)
  216. mHandle->releaseVideoFocus();
  217. return true;
  218. }
  219. bool AutoLink::get_audio_focus()
  220. {
  221. if(mHandle)
  222. mHandle->getAudioFocus();
  223. return true;
  224. }
  225. bool AutoLink::release_audio_focus()
  226. {
  227. if(mHandle)
  228. mHandle->releaseAudioFocus();
  229. return true;
  230. }
  231. void AutoLink::set_inserted(bool inserted, PhoneType phoneType)
  232. {
  233. mPhoneType = phoneType;
  234. }
  235. void AutoLink::send_screen_size(int width, int height)
  236. {
  237. }
  238. void AutoLink::record_audio_callback(unsigned char *data, int len)
  239. {
  240. std::lock_guard<std::mutex> lock(mMutex);
  241. mRecData += string((char*)data, len);
  242. }
  243. void AutoLink::send_car_bluetooth(const string& name, const string& address, const string& pin)
  244. {
  245. mbtaddress = address;
  246. }
  247. void AutoLink::send_phone_bluetooth(const string& address)
  248. {
  249. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  250. }
  251. void AutoLink::send_touch(int x, int y, TouchCode touchCode)
  252. {
  253. printf("auto x:%d, y:%d, press:%d\r\n",x, y, touchCode);
  254. if(mHandle){
  255. if(touchCode == Touch_Up)
  256. mHandle->sendTouchEvent(x, y, 1);
  257. else if(touchCode == Touch_Press)
  258. mHandle->sendTouchEvent(x, y, 0);
  259. else if(touchCode == Touch_Move)
  260. mHandle->sendTouchEvent(x, y, 2);
  261. }
  262. }
  263. bool AutoLink::send_key(KeyCode keyCode)
  264. {
  265. AutoKeyCode ret;
  266. switch(keyCode) {
  267. case KYE_HOME:
  268. ret = KEYCODE_HOME;
  269. break;
  270. case KEY_SYSTEM_HOME:
  271. ret = KEYCODE_MENU;
  272. break;
  273. case KEY_SYSTEM_BACK:
  274. ret = KEYCODE_BACK;
  275. break;
  276. case KEY_VOICE_ASSISTANT:
  277. ret = KEYCODE_SEARCH;
  278. break;
  279. case KEY_MUSIC_PLAY_PAUSE:
  280. ret = KEYCODE_MEDIA_PLAY_PAUSE;
  281. break;
  282. case KEY_MUSIC_PLAY:
  283. ret = KEYCODE_MEDIA_PLAY;
  284. break;
  285. case KEY_MUSIC_PAUSE:
  286. ret = KEYCODE_MEDIA_STOP;
  287. break;
  288. case KEY_MUSIC_NEXT:
  289. ret = KEYCODE_MEDIA_NEXT;
  290. break;
  291. case KEY_MUSIC_PREVIOUS:
  292. ret = KEYCODE_MEDIA_PREVIOUS;
  293. break;
  294. case KEY_PHONE:
  295. {
  296. static bool call = false;
  297. if(!call)
  298. {
  299. ret = KEYCODE_CALL;
  300. }else
  301. {
  302. ret = KEYCODE_ENDCALL;
  303. }
  304. call = !call;
  305. }
  306. break;
  307. case KEY_LIGHTMODE:
  308. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  309. mHandle->setConfig("night_mode", 0);
  310. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  311. return true;
  312. case KEY_NIGHTMODE:
  313. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  314. mHandle->setConfig("night_mode", 1);
  315. return true;
  316. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  317. default:
  318. break;
  319. }
  320. if(ret == KEY_CAR_BACK){
  321. set_background();
  322. }
  323. else if(ret == KEY_CAR_FRONT){
  324. set_foreground();
  325. }
  326. else
  327. {
  328. mHandle->sendKeyEvent(ret, true);
  329. usleep(20000);
  330. mHandle->sendKeyEvent(ret, false);
  331. }
  332. }
  333. bool AutoLink::send_wheel(WheelCode wheel, bool foucs)
  334. {
  335. if (!foucs) {
  336. mHandle->sendKeyEvent(KEYCODE_DPAD_CENTER, true);
  337. usleep(20000);
  338. mHandle->sendKeyEvent(KEYCODE_DPAD_CENTER, false);
  339. } else {
  340. mHandle->sendKnobEvent(KEYCODE_ROTARY_CONTROLLER, wheel);
  341. }
  342. }
  343. bool AutoLink::send_night_mode(bool night)
  344. {
  345. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  346. if(mHandle){
  347. mHandle->setConfig("night_mode", night);
  348. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  349. }
  350. return true;
  351. }
  352. bool AutoLink::send_right_hand_driver(bool right)
  353. {
  354. if(mHandle)
  355. mHandle->setConfig("driver_position", right);
  356. return true;
  357. }
  358. bool AutoLink::open_page(AppPage appPage)
  359. {
  360. int ret = 0;
  361. if(appPage == APP_PAGE_NAVIGATION){
  362. ret = KEYCODE_NAVIGATION;
  363. }
  364. else if(appPage == APP_PAGE_MAIN){
  365. ret = KEYCODE_HOME;
  366. }
  367. else if(appPage == APP_PAGE_MUSIC){
  368. ret = KEYCODE_MUSIC;
  369. }
  370. mHandle->sendKeyEvent(ret, true);
  371. usleep(20000);
  372. mHandle->sendKeyEvent(ret, false);
  373. }
  374. void AutoLink::request_status(RequestAppStatus requestAppStatus, void *reserved)
  375. {
  376. }
  377. void AutoLink::setRecData(string str)
  378. {
  379. mRecData = str;
  380. }
  381. #endif