DB_SEC_E_AUTH_FAILED 错误

本文介绍了一种常见的数据库连接错误DB_SEC_E_AUTH_FAILED,分析了错误产生的原因,并给出了解决方案。错误源于数据库连接字符串配置不当,导致Connection为空值,进而引发访问数据库失败。
部署运行你感兴趣的模型镜像
在调试程序的时候出现 DB_SEC_E_AUTH_FAILED 错误,没有任何提示是哪里的错误,在网上找了,有提示是数据库连接有问题,回来仔细分析代码,发现 Connection 中的字符串有问题,导致Connection 是空值,而后边也没有进行空值处理,而是直接访问数据库,造成了上面的错误。

您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

==> /var/log/gitlab/gitlab-rails/exceptions_json.log <====> /var/log/gitlab/gitlab-rails/application.log <==2025-08-28T04:45:08.052Z: {:message=>"Dropping detached postgres partitions"}2025-08-28T04:45:08.053Z: {:message=>"Switched database connection", :connection_name=>"main"}2025-08-28T04:45:08.053Z: {:message=>"Checking for previously detached partitions to drop"}2025-08-28T04:45:08.144Z: {:message=>"Finished dropping detached postgres partitions"}2025-08-28T04:45:08.144Z: {:message=>"Switched database connection", :connection_name=>"main"}2025-08-28T04:45:08.237Z: {:message=>"Switched database connection", :connection_name=>"main"}2025-08-28T04:45:08.266Z: {:message=>"Switched database connection", :connection_name=>"main"}2025-08-28T04:45:08.386Z: {:message=>"Switched database connection", :connection_name=>"main"}2025-08-28T05:20:37.702Z: {:message=>"Excluding unhealthy shards", :failed_checks=>[{:status=>"failed", :message=>"7:permission denied. debug_error_string:{\"created\":\"@1756358407.690139809\",\"description\":\"Error received from peer unix:/var/opt/gitlab/gitaly/gitaly.socket\",\"file\":\"src/core/lib/surface/call.cc\",\"file_line\":1063,\"grpc_message\":\"permission denied\",\"grpc_status\":7}", :labels=>{:shard=>"gitaly-2"}}, {:status=>"failed", :message=>"gitaly node connectivity & disk access: the following nodes are not healthy: tcp://192.168.41.3:8075", :labels=>{:shard=>"default"}}], :class=>"RepositoryCheck::DispatchWorker"}2025-08-28T05:40:08.600Z: Ci::StuckBuilds::DropScheduledService: Cleaning scheduled, timed-out builds==> /var/log/gitlab/gitlab-rails/grpc.log <====> /var/log/gitlab/gitlab-rails/git_json.log <====> /var/log/gitlab/gitlab-rails/auth.log <====> /var/log/gitlab/gitlab-rails/api_json.log <====> /var/log/gitlab/gitlab-rails/service_measurement.log <====> /var/log/gitlab/gitlab-rails/database_load_balancing.log <====> /var/log/gitlab/gitlab-rails/production_json.log <=={"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:09:57.670Z","params":[],"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.098239,"mem_objects":1992,"mem_bytes":867576,"mem_mallocs":4906,"mem_total_bytes":947256,"pid":29061,"worker_id":"puma_2","rate_limiting_gates":[],"correlation_id":"7710a24b-ad5e-45c2-b3fb-731d8e630232","db_duration_s":0.0,"view_duration_s":0.00095,"duration_s":0.08899}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:10:12.646Z","params":[],"redis_calls":3,"redis_duration_s":0.00208,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.00208,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.07088,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29064,"worker_id":"puma_3","rate_limiting_gates":[],"correlation_id":"acab1e51-5825-47d1-96dc-1faa70268d7a","db_duration_s":0.0,"view_duration_s":0.00102,"duration_s":0.05915}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:10:27.640Z","params":[],"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.066442,"mem_objects":1992,"mem_bytes":867576,"mem_mallocs":4906,"mem_total_bytes":947256,"pid":29064,"worker_id":"puma_3","rate_limiting_gates":[],"correlation_id":"8151f690-67a9-478a-a6cb-19d703acc7c5","db_duration_s":0.0,"view_duration_s":0.00098,"duration_s":0.05607}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:10:42.640Z","params":[],"redis_calls":3,"redis_duration_s":0.001468,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.001468,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.066602,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29053,"worker_id":"puma_0","rate_limiting_gates":[],"correlation_id":"e0a6709c-3d88-4d2c-9b5f-c6a220ecb137","db_duration_s":0.0,"view_duration_s":0.00093,"duration_s":0.05519}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:10:57.633Z","params":[],"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.059788,"mem_objects":1992,"mem_bytes":867576,"mem_mallocs":4906,"mem_total_bytes":947256,"pid":29053,"worker_id":"puma_0","rate_limiting_gates":[],"correlation_id":"5a3bfca8-2182-4000-9ede-11d89bbf155c","db_duration_s":0.0,"view_duration_s":0.00085,"duration_s":0.05267}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:11:12.642Z","params":[],"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.068716,"mem_objects":1992,"mem_bytes":867576,"mem_mallocs":4906,"mem_total_bytes":947256,"pid":29053,"worker_id":"puma_0","rate_limiting_gates":[],"correlation_id":"ac47d522-332d-4b1d-9d3a-d855ff239712","db_duration_s":0.0,"view_duration_s":0.00093,"duration_s":0.05931}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:11:27.659Z","params":[],"redis_calls":3,"redis_duration_s":0.002131,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.002131,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.083974,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29064,"worker_id":"puma_3","rate_limiting_gates":[],"correlation_id":"2667866f-fcbe-4817-ab97-7df209d37897","db_duration_s":0.0,"view_duration_s":0.00146,"duration_s":0.06867}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:11:42.648Z","params":[],"redis_calls":3,"redis_duration_s":0.001244,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.001244,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.075447,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29057,"worker_id":"puma_1","rate_limiting_gates":[],"correlation_id":"1f51f1b2-c732-4cee-927f-480ae2563dd4","db_duration_s":0.0,"view_duration_s":0.00118,"duration_s":0.06432}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:11:57.647Z","params":[],"redis_calls":3,"redis_duration_s":0.001642,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.001642,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.07402,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29061,"worker_id":"puma_2","rate_limiting_gates":[],"correlation_id":"91a88641-1e5f-4c61-b7f6-b58c9ba15e68","db_duration_s":0.0,"view_duration_s":0.00082,"duration_s":0.0627}{"method":"GET","path":"/-/metrics","format":"html","controller":"MetricsController","action":"index","status":200,"time":"2025-08-28T06:12:12.650Z","params":[],"redis_calls":3,"redis_duration_s":0.00147,"redis_read_bytes":608,"redis_write_bytes":206,"redis_cache_calls":3,"redis_cache_duration_s":0.00147,"redis_cache_read_bytes":608,"redis_cache_write_bytes":206,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.076819,"mem_objects":2529,"mem_bytes":1023080,"mem_mallocs":5170,"mem_total_bytes":1124240,"pid":29053,"worker_id":"puma_0","rate_limiting_gates":[],"correlation_id":"90764384-aa97-40bb-9d13-f381f8285583","db_duration_s":0.0,"view_duration_s":0.0012,"duration_s":0.06532}==> /var/log/gitlab/gitlab-rails/sidekiq_client.log <====> /var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-2025-08-09-16-03-52.log <====> /var/log/gitlab/gitlab-rails/application_json.log <=={"severity":"INFO","time":"2025-08-28T04:45:08.052Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Dropping detached postgres partitions"}{"severity":"DEBUG","time":"2025-08-28T04:45:08.053Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Switched database connection","connection_name":"main"}{"severity":"INFO","time":"2025-08-28T04:45:08.053Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Checking for previously detached partitions to drop"}{"severity":"INFO","time":"2025-08-28T04:45:08.144Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Finished dropping detached postgres partitions"}{"severity":"DEBUG","time":"2025-08-28T04:45:08.144Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Switched database connection","connection_name":"main"}{"severity":"DEBUG","time":"2025-08-28T04:45:08.238Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Switched database connection","connection_name":"main"}{"severity":"DEBUG","time":"2025-08-28T04:45:08.266Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Switched database connection","connection_name":"main"}{"severity":"DEBUG","time":"2025-08-28T04:45:08.387Z","correlation_id":"f49f6fc97aa56ff192e961d548cef24d","message":"Switched database connection","connection_name":"main"}{"severity":"ERROR","time":"2025-08-28T05:20:37.702Z","correlation_id":"5e2caba4a96c082232b68c79ad5d9c34","message":"Excluding unhealthy shards","failed_checks":[{"status":"failed","message":"7:permission denied. debug_error_string:{\"created\":\"@1756358407.690139809\",\"description\":\"Error received from peer unix:/var/opt/gitlab/gitaly/gitaly.socket\",\"file\":\"src/core/lib/surface/call.cc\",\"file_line\":1063,\"grpc_message\":\"permission denied\",\"grpc_status\":7}","labels":{"shard":"gitaly-2"}},{"status":"failed","message":"gitaly node connectivity \u0026 disk access: the following nodes are not healthy: tcp://192.168.41.3:8075","labels":{"shard":"default"}}],"class":"RepositoryCheck::DispatchWorker"}{"severity":"INFO","time":"2025-08-28T05:40:08.600Z","correlation_id":"996ed303f842c5f001a402c9e726bdbb","message":"Ci::StuckBuilds::DropScheduledService: Cleaning scheduled, timed-out builds"}==> /var/log/gitlab/gitlab-rails/production.log <==Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:15 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:16 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:17 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:18 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:19 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:20 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:21 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:22 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:23 +0800Started GET "/-/health" for 127.0.0.1 at 2025-08-28 14:12:24 +0800==> /var/log/gitlab/prometheus/current <==2025-08-28_03:57:30.77464 ts=2025-08-28T03:57:30.774Z caller=checkpoint.go:100 level=info component=tsdb msg="Creating checkpoint" from_segment=214 to_segment=215 mint=17562168000002025-08-28_03:57:30.93603 ts=2025-08-28T03:57:30.935Z caller=head.go:1013 level=info component=tsdb msg="WAL checkpoint complete" first=214 last=215 duration=161.418949ms2025-08-28_03:57:32.87013 ts=2025-08-28T03:57:32.870Z caller=manager.go:213 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to load specified CA cert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-cadvisor2025-08-28_03:57:32.87017 ts=2025-08-28T03:57:32.870Z caller=manager.go:213 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to load specified CA cert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-nodes2025-08-28_03:57:32.87040 ts=2025-08-28T03:57:32.870Z caller=manager.go:213 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to load specified CA cert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-pods2025-08-28_05:57:32.13015 ts=2025-08-28T05:57:32.129Z caller=compact.go:519 level=info component=tsdb msg="write block" mint=1756349850661 maxt=1756353600000 ulid=01K3QM1CY67XX3HNZ43AT89XEM duration=411.731549ms2025-08-28_05:57:32.14118 ts=2025-08-28T05:57:32.141Z caller=db.go:1294 level=info component=tsdb msg="Deleting obsolete block" block=01K2D169FEGBGWT15E3SG4EECN2025-08-28_05:57:32.14605 ts=2025-08-28T05:57:32.145Z caller=db.go:1294 level=info component=tsdb msg="Deleting obsolete block" block=01K2GWSC54DD7BBKBVNC8NSPM02025-08-28_05:57:32.15102 ts=2025-08-28T05:57:32.150Z caller=db.go:1294 level=info component=tsdb msg="Deleting obsolete block" block=01K2EYZW7RMFE1D0FD6YTRFB3N2025-08-28_05:57:32.18483 ts=2025-08-28T05:57:32.184Z caller=head.go:844 level=info component=tsdb msg="Head GC completed" duration=33.616389ms==> /var/log/gitlab/prometheus/state <====> /var/log/gitlab/puma/puma_stderr.log <====> /var/log/gitlab/puma/puma_stdout.log <====> /var/log/gitlab/puma/current <==2025-08-28_03:57:29.57607 {"timestamp":"2025-08-28T03:57:29.576Z","pid":25248,"message":"* Environment: production"}2025-08-28_03:57:29.57610 {"timestamp":"2025-08-28T03:57:29.576Z","pid":25248,"message":"* Master PID: 25248"}2025-08-28_03:57:29.57611 {"timestamp":"2025-08-28T03:57:29.576Z","pid":25248,"message":"* Workers: 4"}2025-08-28_03:57:29.57616 {"timestamp":"2025-08-28T03:57:29.576Z","pid":25248,"message":"* Restarts: (鉁�) hot (鉁�) phased"}2025-08-28_03:57:29.57616 {"timestamp":"2025-08-28T03:57:29.576Z","pid":25248,"message":"* Preloading application"}2025-08-28_03:58:43.04424 {"timestamp":"2025-08-28T03:58:43.043Z","pid":25248,"message":"* Listening on unix:///var/opt/gitlab/gitlab-rails/sockets/gitlab.socket"}2025-08-28_03:58:43.04504 {"timestamp":"2025-08-28T03:58:43.044Z","pid":25248,"message":"* Listening on http://127.0.0.1:8080"}2025-08-28_03:58:43.04515 {"timestamp":"2025-08-28T03:58:43.045Z","pid":25248,"message":"! WARNING: Detected 1 Thread(s) started in app boot:"}2025-08-28_03:58:43.04528 {"timestamp":"2025-08-28T03:58:43.045Z","pid":25248,"message":"! #\u003cThread:0x00007fe68f0ea588 /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/rack-timeout-0.6.3/lib/rack/timeout/support/scheduler.rb:73 sleep\u003e - /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/rack-timeout-0.6.3/lib/rack/timeout/support/scheduler.rb:91:in `sleep'"}2025-08-28_03:58:43.04543 {"timestamp":"2025-08-28T03:58:43.045Z","pid":25248,"message":"Use Ctrl-C to stop"}==> /var/log/gitlab/puma/state <====> /var/log/gitlab/gitlab-kas/current <==2025-08-28_03:54:24.78884 {"level":"info","time":"2025-08-28T11:54:24.788+0800","msg":"Private API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8155"}2025-08-28_03:54:24.78891 {"level":"info","time":"2025-08-28T11:54:24.788+0800","msg":"API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8153"}2025-08-28_03:54:24.78892 {"level":"info","time":"2025-08-28T11:54:24.788+0800","msg":"Agentk API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8150","is_websocket":true}2025-08-28_03:54:24.78893 {"level":"info","time":"2025-08-28T11:54:24.788+0800","msg":"Observability endpoint is up","mod_name":"observability","net_network":"tcp","net_address":"127.0.0.1:8151"}2025-08-28_03:54:24.78893 {"level":"info","time":"2025-08-28T11:54:24.788+0800","msg":"Kubernetes API endpoint is up","mod_name":"kubernetes_api","net_network":"tcp","net_address":"127.0.0.1:8154"}2025-08-28_03:57:24.87723 {"level":"info","time":"2025-08-28T11:57:24.877+0800","msg":"Private API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8155"}2025-08-28_03:57:24.87727 {"level":"info","time":"2025-08-28T11:57:24.877+0800","msg":"Kubernetes API endpoint is up","mod_name":"kubernetes_api","net_network":"tcp","net_address":"127.0.0.1:8154"}2025-08-28_03:57:24.87728 {"level":"info","time":"2025-08-28T11:57:24.877+0800","msg":"API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8153"}2025-08-28_03:57:24.87728 {"level":"info","time":"2025-08-28T11:57:24.877+0800","msg":"Agentk API endpoint is up","net_network":"tcp","net_address":"127.0.0.1:8150","is_websocket":true}2025-08-28_03:57:24.87744 {"level":"info","time":"2025-08-28T11:57:24.877+0800","msg":"Observability endpoint is up","mod_name":"observability","net_network":"tcp","net_address":"127.0.0.1:8151"}==> /var/log/gitlab/gitlab-kas/state <====> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-28-11-56-22.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-09-16-03-47.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-25-16-02-47.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-09-19-26-13.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/current <=={"component":"HealthManager","correlation_id":"01K3QMW441YR5FPC7281XC8428","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:07.425Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMW60ZRCRGG5A8CCWG7KFS","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:09.375Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMW7V9SG05V7RTV1FWGZC5","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:11.241Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMW9KMRXV0PBDZK1H7AH5J","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:13.045Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWBHRFXSF6F2N002A5AT3","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:15.033Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWDDMN13NJGVYHMWFZXD4","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:16.948Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWF6JZP9K1Z9W0TV43XH1","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:18.770Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWH0M10MF7FK1AVW6R663","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:20.628Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWJYMJZBZ9WQD24YZRWM2","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:22.613Z","virtual_storage":"default"}{"component":"HealthManager","correlation_id":"01K3QMWMY57263MBG9SCS66EZY","error":"rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp 192.168.41.3:8075: connect: connection refused\"","level":"error","msg":"failed checking node health","pid":25138,"storage":"gitaly-3","time":"2025-08-28T06:12:24.646Z","virtual_storage":"default"}==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-26-12-08-33.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-09-22-20-50.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/state <====> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-09-21-38-32.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/praefect/praefect-sql-migrate-2025-08-25-19-09-07.log <==praefect sql-migrate: all migrations are up==> /var/log/gitlab/sidekiq/current <=={"severity":"INFO","time":"2025-08-28T06:11:05.493Z","retry":0,"queue":"cronjob:schedule_merge_request_cleanup_refs","version":0,"queue_namespace":"cronjob","args":[],"class":"ScheduleMergeRequestCleanupRefsWorker","jid":"45ab0f892d791776de9a0b42","created_at":"2025-08-28T06:11:05.467Z","meta.caller_id":"Cronjob","correlation_id":"e6f219114252350a4ccfc656a401e2cc","meta.root_caller_id":"Cronjob","meta.feature_category":"code_review","worker_data_consistency":"always","idempotency_key":"resque:gitlab:duplicate:cronjob:schedule_merge_request_cleanup_refs:33e8a9dcd4c9780ad0ea123ad7ccbabde1aa1e90ffcbb928434ba4b5800a5811","size_limiter":"validated","enqueued_at":"2025-08-28T06:11:05.469Z","job_size_bytes":2,"pid":25493,"message":"ScheduleMergeRequestCleanupRefsWorker JID-45ab0f892d791776de9a0b42: done: 0.016838 sec","job_status":"done","scheduling_latency_s":0.007451,"redis_calls":3,"redis_duration_s":0.002085,"redis_read_bytes":204,"redis_write_bytes":283,"redis_cache_calls":1,"redis_cache_duration_s":0.000723,"redis_cache_read_bytes":202,"redis_cache_write_bytes":61,"redis_queues_calls":2,"redis_queues_duration_s":0.001362,"redis_queues_read_bytes":2,"redis_queues_write_bytes":222,"db_count":0,"db_write_count":0,"db_cached_count":0,"db_replica_count":0,"db_primary_count":0,"db_main_count":0,"db_main_replica_count":0,"db_replica_cached_count":0,"db_primary_cached_count":0,"db_main_cached_count":0,"db_main_replica_cached_count":0,"db_replica_wal_count":0,"db_primary_wal_count":0,"db_main_wal_count":0,"db_main_replica_wal_count":0,"db_replica_wal_cached_count":0,"db_primary_wal_cached_count":0,"db_main_wal_cached_count":0,"db_main_replica_wal_cached_count":0,"db_replica_duration_s":0.0,"db_primary_duration_s":0.0,"db_main_duration_s":0.0,"db_main_replica_duration_s":0.0,"cpu_s":0.010796,"mem_objects":1471,"mem_bytes":122488,"mem_mallocs":380,"mem_total_bytes":181328,"worker_id":"sidekiq_0","rate_limiting_gates":[],"duration_s":0.016838,"completed_at":"2025-08-28T06:11:05.493Z","load_balancing_strategy":"primary","db_duration_s":0.0}{"severity":"INFO","time":"2025-08-28T06:12:04.467Z","retry":0,"queue":"cronjob:users_migrate_records_to_ghost_user_in_batches","version":0,"queue_namespace":"cronjob","args":[],"class":"Users::MigrateRecordsToGhostUserInBatchesWorker","jid":"bdba58df550f25a5c4502ce3","created_at":"2025-08-28T06:12:04.425Z","meta.caller_id":"Cronjob","correlation_id":"f3325d7eef2e720bda14b20f84b55d23","meta.root_caller_id":"Cronjob","meta.feature_category":"users","worker_data_consistency":"always","idempotency_key":"resque:gitlab:duplicate:cronjob:users_migrate_records_to_ghost_user_in_batches:4bdb3193c92ce2ad4e73652ad55816e507f3dd7f07576150fc7c08572353b9e8","size_limiter":"validated","enqueued_at":"2025-08-28T06:12:04.462Z","job_size_bytes":2,"pid":25493,"message":"Users::MigrateRecordsToGhostUserInBatchesWorker JID-bdba58df550f25a5c4502ce3: start","job_status":"start","scheduling_latency_s":0.005215}{"severity":"INFO","time":"2025-08-28T06:12:04.631Z","retry":0,"queue":"cronjob:database_batched_background_migration_ci_database","version":0,"queue_namespace":"cronjob","args":[],"class":"Database::BatchedBackgroundMigration::CiDatabaseWorker","jid":"b8e2653e2bbe232da2228b57","created_at":"2025-08-28T06:12:04.622Z","meta.caller_id":"Cronjob","correlation_id":"f89e8db756fe29aef54362387433b49c","meta.root_caller_id":"Cronjob","meta.feature_category":"database","worker_data_consistency":"always","idempotency_key":"resque:gitlab:duplicate:cronjob:database_batched_background_migration_ci_database:6ba8adee4a8c1e77d2f087a2765c43226ceffa1fd65abc34b95725a7c9abd857","enqueued_at":"2025-08-28T06:12:04.627Z","job_size_bytes":2,"pid":25493,"message":"Database::BatchedBackgroundMigration::CiDatabaseWorker JID-b8e2653e2bbe232da2228b57: start","job_status":"start","scheduling_latency_s":0.004391}{"severity":"INFO","time":"2025-08-28T06:12:04.655Z","class":"Database::BatchedBackgroundMigration::CiDatabaseWorker","database":"ci","message":"skipping migration execution for unconfigured database","retry":0}{"severity":"INFO","time":"2025-08-28T06:12:04.664Z","retry":0,"queue":"cronjob:database_batched_background_migration_ci_database","version":0,"queue_namespace":"cronjob","args":[],"class":"Database::BatchedBackgroundMigration::CiDatabaseWorker","jid":"b8e2653e2bbe232da2228b57","created_at":"2025-08-28T06:12:04.622Z","meta.caller_id":"Cronjob","correlation_id":"f89e8db756fe29aef54362387433b49c","meta.root_caller_id":"Cronjob","meta.feature_category":"database","worker_data_consistency":"always","idempotency_key":"resque:gitlab:duplicate:cronjob:database_batched_background_migration_ci_database:6ba8adee4a8c1e77d2f087a2765c43226ceffa1fd65abc34b95725a7c9abd857","enqueued_at":"2025-08-28T06:12:04.627Z","job_size_bytes":2,"pid":25493,"message":"Database::BatchedBackgroundMigration::CiDatabaseWorker JID-b8e2653e2bbe232da2228b57: done: 0.032335 sec","job_status":"done","scheduling_latency_s":0.004391,"redis_calls":2,"redis_duration_s":0.006605,"redis_read_bytes":2,"redis_write_bytes":236,"redis_queues_calls":2,"redis_queues_duration_s":0.006605,"redis_queues_read_bytes
最新发布
08-29
#include "Dri_BT.h" // 定义蓝牙状态 static bool bt_initialized = false; /* 定义esp32收到手机数据时的回调弱函数 */ void __attribute__((weak)) App_Communication_DealBtData(uint8_t *data, uint16_t dataLen) { } // 设置蓝牙名称 static char example_device_name[ESP_BLE_ADV_NAME_LEN_MAX] = "Zkevin_SmartHumidifier"; static uint8_t adv_config_done = 0; static uint16_t heart_rate_handle_table[HRS_IDX_NB]; static uint8_t test_manufacturer[3] = {'E', 'S', 'P'}; static uint8_t sec_service_uuid[16] = { /* LSB <--------------------------------------------------------------------------------> MSB */ // first uuid, 16bit, [12],[13] is the value 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x18, 0x0D, 0x00, 0x00, }; // config adv data static esp_ble_adv_data_t heart_rate_adv_config = { .set_scan_rsp = false, .include_txpower = true, .min_interval = 0x0006, // slave connection min interval, Time = min_interval * 1.25 msec .max_interval = 0x0010, // slave connection max interval, Time = max_interval * 1.25 msec .appearance = 0x00, .manufacturer_len = 0, // TEST_MANUFACTURER_DATA_LEN, .p_manufacturer_data = NULL, //&test_manufacturer[0], .service_data_len = 0, .p_service_data = NULL, .service_uuid_len = sizeof(sec_service_uuid), .p_service_uuid = sec_service_uuid, .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT), }; // config scan response data static esp_ble_adv_data_t heart_rate_scan_rsp_config = { .set_scan_rsp = true, .include_name = true, .manufacturer_len = sizeof(test_manufacturer), .p_manufacturer_data = test_manufacturer, }; static esp_ble_adv_params_t heart_rate_adv_params = { .adv_int_min = 0x100, .adv_int_max = 0x100, .adv_type = ADV_TYPE_IND, .own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, }; struct gatts_profile_inst { esp_gatts_cb_t gatts_cb; uint16_t gatts_if; uint16_t app_id; uint16_t conn_id; uint16_t service_handle; esp_gatt_srvc_id_t service_id; uint16_t char_handle; esp_bt_uuid_t char_uuid; esp_gatt_perm_t perm; esp_gatt_char_prop_t property; uint16_t descr_handle; esp_bt_uuid_t descr_uuid; }; static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); /* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */ static struct gatts_profile_inst heart_rate_profile_tab[HEART_PROFILE_NUM] = { [HEART_PROFILE_APP_IDX] = { .gatts_cb = gatts_profile_event_handler, .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ }, }; /* * Heart Rate PROFILE ATTRIBUTES **************************************************************************************** */ /// Heart Rate Sensor Service static const uint16_t heart_rate_svc = ESP_GATT_UUID_HEART_RATE_SVC; #define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE; static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE; static const uint16_t character_client_config_uuid = ESP_GATT_UUID_CHAR_CLIENT_CONFIG; static const uint8_t char_prop_notify = ESP_GATT_CHAR_PROP_BIT_NOTIFY; static const uint8_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ; static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_READ; /// Heart Rate Sensor Service - Heart Rate Measurement Characteristic, notify static const uint16_t heart_rate_meas_uuid = ESP_GATT_HEART_RATE_MEAS; static const uint8_t heart_measurement_ccc[2] = {0x00, 0x00}; /// Heart Rate Sensor Service -Body Sensor Location characteristic, read static const uint16_t body_sensor_location_uuid = ESP_GATT_BODY_SENSOR_LOCATION; static const uint8_t body_sensor_loc_val[1] = {0x00}; /// Heart Rate Sensor Service - Heart Rate Control Point characteristic, write&read static const uint16_t heart_rate_ctrl_point = ESP_GATT_HEART_RATE_CNTL_POINT; static const uint8_t heart_ctrl_point[1] = {0x00}; /// Full HRS Database Description - Used to add attributes into the database static const esp_gatts_attr_db_t heart_rate_gatt_db[HRS_IDX_NB] = { // Heart Rate Service Declaration [HRS_IDX_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, sizeof(uint16_t), sizeof(heart_rate_svc), (uint8_t *)&heart_rate_svc}}, // Heart Rate Measurement Characteristic Declaration [HRS_IDX_HR_MEAS_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_notify}}, // Heart Rate Measurement Characteristic Value [HRS_IDX_HR_MEAS_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&heart_rate_meas_uuid, ESP_GATT_PERM_READ, HRPS_HT_MEAS_MAX_LEN, 0, NULL}}, // Heart Rate Measurement Characteristic - Client Characteristic Configuration Descriptor [HRS_IDX_HR_MEAS_NTF_CFG] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(heart_measurement_ccc), (uint8_t *)heart_measurement_ccc}}, // Body Sensor Location Characteristic Declaration [HRS_IDX_BOBY_SENSOR_LOC_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}}, // Body Sensor Location Characteristic Value [HRS_IDX_BOBY_SENSOR_LOC_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&body_sensor_location_uuid, ESP_GATT_PERM_READ_ENCRYPTED, sizeof(uint8_t), sizeof(body_sensor_loc_val), (uint8_t *)body_sensor_loc_val}}, // Heart Rate Control Point Characteristic Declaration [HRS_IDX_HR_CTNL_PT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}}, // Heart Rate Control Point Characteristic Value [HRS_IDX_HR_CTNL_PT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&heart_rate_ctrl_point, ESP_GATT_PERM_WRITE_ENCRYPTED | ESP_GATT_PERM_READ_ENCRYPTED, sizeof(uint8_t), sizeof(heart_ctrl_point), (uint8_t *)heart_ctrl_point}}, }; static char *esp_key_type_to_str(esp_ble_key_type_t key_type) { char *key_str = NULL; switch (key_type) { case ESP_LE_KEY_NONE: key_str = "ESP_LE_KEY_NONE"; break; case ESP_LE_KEY_PENC: key_str = "ESP_LE_KEY_PENC"; break; case ESP_LE_KEY_PID: key_str = "ESP_LE_KEY_PID"; break; case ESP_LE_KEY_PCSRK: key_str = "ESP_LE_KEY_PCSRK"; break; case ESP_LE_KEY_PLK: key_str = "ESP_LE_KEY_PLK"; break; case ESP_LE_KEY_LLK: key_str = "ESP_LE_KEY_LLK"; break; case ESP_LE_KEY_LENC: key_str = "ESP_LE_KEY_LENC"; break; case ESP_LE_KEY_LID: key_str = "ESP_LE_KEY_LID"; break; case ESP_LE_KEY_LCSRK: key_str = "ESP_LE_KEY_LCSRK"; break; default: key_str = "INVALID BLE KEY TYPE"; break; } return key_str; } static char *esp_auth_req_to_str(esp_ble_auth_req_t auth_req) { char *auth_str = NULL; switch (auth_req) { case ESP_LE_AUTH_NO_BOND: auth_str = "ESP_LE_AUTH_NO_BOND"; break; case ESP_LE_AUTH_BOND: auth_str = "ESP_LE_AUTH_BOND"; break; case ESP_LE_AUTH_REQ_MITM: auth_str = "ESP_LE_AUTH_REQ_MITM"; break; case ESP_LE_AUTH_REQ_BOND_MITM: auth_str = "ESP_LE_AUTH_REQ_BOND_MITM"; break; case ESP_LE_AUTH_REQ_SC_ONLY: auth_str = "ESP_LE_AUTH_REQ_SC_ONLY"; break; case ESP_LE_AUTH_REQ_SC_BOND: auth_str = "ESP_LE_AUTH_REQ_SC_BOND"; break; case ESP_LE_AUTH_REQ_SC_MITM: auth_str = "ESP_LE_AUTH_REQ_SC_MITM"; break; case ESP_LE_AUTH_REQ_SC_MITM_BOND: auth_str = "ESP_LE_AUTH_REQ_SC_MITM_BOND"; break; default: auth_str = "INVALID BLE AUTH REQ"; break; } return auth_str; } static void show_bonded_devices(void) { int dev_num = esp_ble_get_bond_device_num(); if (dev_num == 0) { ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number zero\n"); return; } esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); if (!dev_list) { ESP_LOGI(GATTS_TABLE_TAG, "malloc failed, return\n"); return; } esp_ble_get_bond_device_list(&dev_num, dev_list); ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number %d", dev_num); for (int i = 0; i < dev_num; i++) { ESP_LOGI(GATTS_TABLE_TAG, "[%u] addr_type %u, addr " ESP_BD_ADDR_STR "", i, dev_list[i].bd_addr_type, ESP_BD_ADDR_HEX(dev_list[i].bd_addr)); } free(dev_list); } static void __attribute__((unused)) remove_all_bonded_devices(void) { int dev_num = esp_ble_get_bond_device_num(); if (dev_num == 0) { ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number zero\n"); return; } esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); if (!dev_list) { ESP_LOGI(GATTS_TABLE_TAG, "malloc failed, return\n"); return; } esp_ble_get_bond_device_list(&dev_num, dev_list); for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); } free(dev_list); } static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { ESP_LOGV(GATTS_TABLE_TAG, "GAP_EVT, event %d", event); switch (event) { case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: adv_config_done &= (~SCAN_RSP_CONFIG_FLAG); if (adv_config_done == 0) { esp_ble_gap_start_advertising(&heart_rate_adv_params); } break; case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: adv_config_done &= (~ADV_CONFIG_FLAG); if (adv_config_done == 0) { esp_ble_gap_start_advertising(&heart_rate_adv_params); } break; case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: // advertising start complete event to indicate advertising start successfully or failed if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(GATTS_TABLE_TAG, "Advertising start failed, status %x", param->adv_start_cmpl.status); break; } ESP_LOGI(GATTS_TABLE_TAG, "Advertising start successfully"); break; case ESP_GAP_BLE_PASSKEY_REQ_EVT: /* passkey request event */ ESP_LOGI(GATTS_TABLE_TAG, "Passkey request"); /* Call the following function to input the passkey which is displayed on the remote device */ // esp_ble_passkey_reply(heart_rate_profile_tab[HEART_PROFILE_APP_IDX].remote_bda, true, 0x00); break; case ESP_GAP_BLE_OOB_REQ_EVT: { ESP_LOGI(GATTS_TABLE_TAG, "OOB request"); uint8_t tk[16] = {1}; // If you paired with OOB, both devices need to use the same tk esp_ble_oob_req_reply(param->ble_security.ble_req.bd_addr, tk, sizeof(tk)); break; } case ESP_GAP_BLE_LOCAL_IR_EVT: /* BLE local IR event */ ESP_LOGI(GATTS_TABLE_TAG, "Local identity root"); break; case ESP_GAP_BLE_LOCAL_ER_EVT: /* BLE local ER event */ ESP_LOGI(GATTS_TABLE_TAG, "Local encryption root"); break; case ESP_GAP_BLE_NC_REQ_EVT: /* The app will receive this evt when the IO has DisplayYesNO capability and the peer device IO also has DisplayYesNo capability. show the passkey number to the user to confirm it with the number displayed by peer device. */ esp_ble_confirm_reply(param->ble_security.ble_req.bd_addr, true); ESP_LOGI(GATTS_TABLE_TAG, "Numeric Comparison request, passkey %" PRIu32, param->ble_security.key_notif.passkey); break; case ESP_GAP_BLE_SEC_REQ_EVT: /* send the positive(true) security response to the peer device to accept the security request. If not accept the security request, should send the security response with negative(false) accept value*/ esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); break; case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: /// the app will receive this evt when the IO has Output capability and the peer device IO has Input capability. /// show the passkey number to the user to input it in the peer device. ESP_LOGI(GATTS_TABLE_TAG, "Passkey notify, passkey %06" PRIu32, param->ble_security.key_notif.passkey); break; case ESP_GAP_BLE_KEY_EVT: // shows the ble key info share with peer device to the user. ESP_LOGI(GATTS_TABLE_TAG, "Key exchanged, key_type %s", esp_key_type_to_str(param->ble_security.ble_key.key_type)); break; case ESP_GAP_BLE_AUTH_CMPL_EVT: { esp_bd_addr_t bd_addr; memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTS_TABLE_TAG, "Authentication complete, addr_type %u, addr " ESP_BD_ADDR_STR "", param->ble_security.auth_cmpl.addr_type, ESP_BD_ADDR_HEX(bd_addr)); if (!param->ble_security.auth_cmpl.success) { ESP_LOGI(GATTS_TABLE_TAG, "Pairing failed, reason 0x%x", param->ble_security.auth_cmpl.fail_reason); } else { ESP_LOGI(GATTS_TABLE_TAG, "Pairing successfully, auth_mode %s", esp_auth_req_to_str(param->ble_security.auth_cmpl.auth_mode)); } show_bonded_devices(); break; } case ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT: { ESP_LOGD(GATTS_TABLE_TAG, "Bond device remove, status %d, device " ESP_BD_ADDR_STR "", param->remove_bond_dev_cmpl.status, ESP_BD_ADDR_HEX(param->remove_bond_dev_cmpl.bd_addr)); break; } case ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT: if (param->local_privacy_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(GATTS_TABLE_TAG, "Local privacy config failed, status %x", param->local_privacy_cmpl.status); break; } ESP_LOGI(GATTS_TABLE_TAG, "Local privacy config successfully"); esp_err_t ret = esp_ble_gap_config_adv_data(&heart_rate_adv_config); if (ret) { ESP_LOGE(GATTS_TABLE_TAG, "config adv data failed, error code = %x", ret); } else { adv_config_done |= ADV_CONFIG_FLAG; } ret = esp_ble_gap_config_adv_data(&heart_rate_scan_rsp_config); if (ret) { ESP_LOGE(GATTS_TABLE_TAG, "config adv data failed, error code = %x", ret); } else { adv_config_done |= SCAN_RSP_CONFIG_FLAG; } break; default: break; } } static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { ESP_LOGV(GATTS_TABLE_TAG, "event = %x", event); switch (event) { case ESP_GATTS_REG_EVT: ESP_LOGI(GATTS_TABLE_TAG, "GATT server register, status %d, app_id %d, gatts_if %d", param->reg.status, param->reg.app_id, gatts_if); esp_ble_gap_set_device_name(example_device_name); // generate a resolvable random address esp_ble_gap_config_local_privacy(true); esp_ble_gatts_create_attr_tab(heart_rate_gatt_db, gatts_if, HRS_IDX_NB, HEART_RATE_SVC_INST_ID); break; case ESP_GATTS_READ_EVT: break; case ESP_GATTS_WRITE_EVT: ESP_LOGI(GATTS_TABLE_TAG, "Characteristic write, value "); ESP_LOG_BUFFER_HEX(GATTS_TABLE_TAG, param->write.value, param->write.len); App_Communication_DealBtData(param->write.value, param->write.len); break; case ESP_GATTS_EXEC_WRITE_EVT: break; case ESP_GATTS_MTU_EVT: break; case ESP_GATTS_CONF_EVT: break; case ESP_GATTS_UNREG_EVT: break; case ESP_GATTS_DELETE_EVT: break; case ESP_GATTS_START_EVT: break; case ESP_GATTS_STOP_EVT: break; case ESP_GATTS_CONNECT_EVT: ESP_LOGI(GATTS_TABLE_TAG, "Connected, conn_id %u, remote " ESP_BD_ADDR_STR "", param->connect.conn_id, ESP_BD_ADDR_HEX(param->connect.remote_bda)); /* start security connect with peer device when receive the connect event sent by the master */ esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM); break; case ESP_GATTS_DISCONNECT_EVT: ESP_LOGI(GATTS_TABLE_TAG, "Disconnected, remote " ESP_BD_ADDR_STR ", reason 0x%x", ESP_BD_ADDR_HEX(param->disconnect.remote_bda), param->disconnect.reason); /* start advertising again when missing the connect */ esp_ble_gap_start_advertising(&heart_rate_adv_params); break; case ESP_GATTS_OPEN_EVT: break; case ESP_GATTS_CANCEL_OPEN_EVT: break; case ESP_GATTS_CLOSE_EVT: break; case ESP_GATTS_LISTEN_EVT: break; case ESP_GATTS_CONGEST_EVT: break; case ESP_GATTS_CREAT_ATTR_TAB_EVT: { if (param->create.status == ESP_GATT_OK) { if (param->add_attr_tab.num_handle == HRS_IDX_NB) { ESP_LOGI(GATTS_TABLE_TAG, "Attribute table create successfully, num_handle %x", param->add_attr_tab.num_handle); memcpy(heart_rate_handle_table, param->add_attr_tab.handles, sizeof(heart_rate_handle_table)); esp_ble_gatts_start_service(heart_rate_handle_table[HRS_IDX_SVC]); } else { ESP_LOGE(GATTS_TABLE_TAG, "Attribute table create abnormally, num_handle (%d) doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB); } } else { ESP_LOGE(GATTS_TABLE_TAG, "Attribute table create failed, error code = %x", param->create.status); } break; } default: break; } } static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { /* If event is register event, store the gatts_if for each profile */ if (event == ESP_GATTS_REG_EVT) { if (param->reg.status == ESP_GATT_OK) { heart_rate_profile_tab[HEART_PROFILE_APP_IDX].gatts_if = gatts_if; } else { ESP_LOGI(GATTS_TABLE_TAG, "Reg app failed, app_id %04x, status %d", param->reg.app_id, param->reg.status); return; } } do { int idx; for (idx = 0; idx < HEART_PROFILE_NUM; idx++) { if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ gatts_if == heart_rate_profile_tab[idx].gatts_if) { if (heart_rate_profile_tab[idx].gatts_cb) { heart_rate_profile_tab[idx].gatts_cb(event, gatts_if, param); } } } } while (0); } /** * @brief 初始化蓝牙模块 * */ void Dri_BT_Init(void) { // 避免重复初始化 if (bt_initialized) { ESP_LOGI(GATTS_TABLE_TAG, "Bluetooth already initialized"); return; } esp_err_t ret; // 获取当前蓝牙状态 esp_bt_controller_status_t bt_status = esp_bt_controller_get_status(); // 蓝牙未被初始化 if (bt_status == ESP_BT_CONTROLLER_STATUS_IDLE) { // 初始化NVS.为了存入蓝牙连接信息; ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); // 释放经典蓝牙部署 ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); // 初始化蓝牙硬件配置 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); esp_bt_controller_init(&bt_cfg); esp_bt_controller_enable(ESP_BT_MODE_BLE); // 初始化蓝牙应用层软件配置 esp_bluedroid_init(); esp_bluedroid_enable(); } // 蓝牙未初始化 if (!bt_initialized) { // 注册回调函数 esp_ble_gatts_register_callback(gatts_event_handler); esp_ble_gap_register_callback(gap_event_handler); esp_ble_gatts_app_register(ESP_HEART_RATE_APP_ID); // 设置安全参数,未来传输数据需要加密,采用非对称加密方式(公钥和私钥) esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND; // bonding with peer device after authentication esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE; // set the IO capability to No output No input uint8_t key_size = 16; // the key size should be 7~16 bytes uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; // 安全密钥 uint32_t passkey = 123456; uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE; uint8_t oob_support = ESP_BLE_OOB_DISABLE; esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); } bt_initialized = true; } #include "Dri_Wifi.h" // 蓝牙的两种情况 // 二维码扫描前需要配网低功耗蓝牙和自定义GATT低功耗蓝牙 // WIFI配网成功后,只需要自定义GATT低功耗蓝牙 typedef enum { BT_MODE_GATT, // GATT服务模式 BT_MODE_DUAL // 双模式(同时支持配网和GATT) } bt_mode_t; // 默认双模式 static bt_mode_t current_bt_mode = BT_MODE_DUAL; static wifi_conn wifi_success_cb; static const char *TAG = "WIFI"; const int WIFI_CONNECTED_EVENT = BIT0; static EventGroupHandle_t wifi_event_group; #define PROV_QR_VERSION "v1" #define PROV_TRANSPORT_BLE "ble" #define QRCODE_BASE_URL "https://espressif.github.io/esp-jumpstart/qrcode.html" // WIFI各种事件的回调函数 static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { // 记录失败次数 static int retries; // 记录WIFI连接失败次数 static uint8_t wifi_conn_retries = 0; if (event_base == WIFI_PROV_EVENT) // WIFI配置事件 { switch (event_id) { case WIFI_PROV_START: ESP_LOGI(TAG, "Provisioning started"); break; case WIFI_PROV_CRED_RECV: { wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data; ESP_LOGI(TAG, "Received Wi-Fi credentials" "\n\tSSID : %s\n\tPassword : %s", (const char *)wifi_sta_cfg->ssid, (const char *)wifi_sta_cfg->password); break; } case WIFI_PROV_CRED_FAIL: { wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data; ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s" "\n\tPlease reset to factory and retry provisioning", (*reason == WIFI_PROV_STA_AUTH_ERROR) ? "Wi-Fi station authentication failed" : "Wi-Fi access-point not found"); retries++; if (retries >= 5) { ESP_LOGI(TAG, "Failed to connect with provisioned AP, resetting provisioned credentials"); wifi_prov_mgr_reset_sm_state_on_failure(); retries = 0; } break; } case WIFI_PROV_CRED_SUCCESS: ESP_LOGI(TAG, "Provisioning successful"); retries = 0; break; case WIFI_PROV_END: // 配置完成后,取消初始化管理器 wifi_prov_mgr_deinit(); break; default: break; } } else if (event_base == WIFI_EVENT) // WIFI事件 { switch (event_id) { case WIFI_EVENT_STA_START: esp_wifi_connect(); break; case WIFI_EVENT_STA_DISCONNECTED: ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); wifi_conn_retries++; if (wifi_conn_retries >= 10) { // 重置WIFI配置 wifi_prov_mgr_reset_provisioning(); // 重启芯片 esp_restart(); } esp_wifi_connect(); break; default: break; } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) // 连接成功 { // 重置记录WIFI连接失败次数 wifi_conn_retries = 0; // 引用运行回调函数代码 wifi_success_cb(); ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip)); xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); } else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT) // 蓝牙事件 { switch (event_id) { case PROTOCOMM_TRANSPORT_BLE_CONNECTED: ESP_LOGI(TAG, "BLE transport: Connected!"); break; case PROTOCOMM_TRANSPORT_BLE_DISCONNECTED: ESP_LOGI(TAG, "BLE transport: Disconnected!"); break; default: break; } } } static void wifi_init_sta(void) { /* Start Wi-Fi in station mode */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } static void get_device_service_name(char *service_name, size_t max) { uint8_t eth_mac[6]; const char *ssid_prefix = "PROV_"; esp_wifi_get_mac(WIFI_IF_STA, eth_mac); snprintf(service_name, max, "%s%02X%02X%02X", ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]); } esp_err_t custom_prov_data_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen, void *priv_data) { if (inbuf) { ESP_LOGI(TAG, "Received data: %.*s", inlen, (char *)inbuf); } char response[] = "SUCCESS"; *outbuf = (uint8_t *)strdup(response); if (*outbuf == NULL) { ESP_LOGE(TAG, "System out of memory"); return ESP_ERR_NO_MEM; } *outlen = strlen(response) + 1; /* +1 for NULL terminating byte */ return ESP_OK; } static void wifi_prov_print_qr(const char *name, const char *username, const char *pop, const char *transport) { if (!name || !transport) { ESP_LOGW(TAG, "Cannot generate QR code payload. Data missing."); return; } char payload[150] = {0}; snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" ",\"transport\":\"%s\"}", PROV_QR_VERSION, name, transport); ESP_LOGI(TAG, "Scan this QR code from the provisioning application for Provisioning."); esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT(); esp_qrcode_generate(&cfg, payload); ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload); } /** * @brief 初始化WIFI模块 * */ void Dri_Wifi_Init(wifi_conn wifi_conn_success) { // 赋值回调函数 wifi_success_cb = wifi_conn_success; // 初始化NVS,用于保存WIFI名称于Flash esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_init()); } // 初始化TCP/IP ESP_ERROR_CHECK(esp_netif_init()); // 创建事件循环组 ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_event_group = xEventGroupCreate(); // 注册各种事件回调函数 ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_TRANSPORT_BLE_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_SECURITY_SESSION_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); // 根据TCP/IP网络协议,初始化WIFI esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // 初始化自定义GATT蓝牙 Dri_BT_Init(); // 配网配置信息 wifi_prov_mgr_config_t config = { .scheme = wifi_prov_scheme_ble, .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM, }; // 配网初始化 ESP_ERROR_CHECK(wifi_prov_mgr_init(config)); bool provisioned = false; // wifi_prov_mgr_reset_provisioning(); // 检查是否配网 ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned)); // 根据是否配网,做对应操作 // 没有配网 if (!provisioned) { ESP_LOGI(TAG, "Starting provisioning"); // 配网模式:同时支持配网和GATT current_bt_mode = BT_MODE_DUAL; // WIFI设备名称 char service_name[12]; get_device_service_name(service_name, sizeof(service_name)); // 无安全设置 const char *service_key = NULL; // WIFI设备唯一标识 uint8_t custom_service_uuid[] = { /* LSB <--------------------------------------- * ---------------------------------------> MSB */ 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, }; wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid); // 创建WIFI设备节点信息 wifi_prov_mgr_endpoint_create("custom-data"); // 启动配网设备 wifi_prov_security_t security = WIFI_PROV_SECURITY_0; ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, (const void *)NULL, service_name, service_key)); // 在网络中进行设备注册 wifi_prov_mgr_endpoint_register("custom-data", custom_prov_data_handler, NULL); // 打印二维码 wifi_prov_print_qr(service_name, NULL, NULL, PROV_TRANSPORT_BLE); // 启动双服务广播 start_dual_service_advertising(); } else { ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA"); // 已配网:仅GATT模式 current_bt_mode = BT_MODE_GATT; // 配网初始化 wifi_prov_mgr_deinit(); // 启动WIFI wifi_init_sta(); // 启动GATT服务广播 esp_ble_gap_start_advertising(&heart_rate_adv_params); } // 等待WIFI连接成功 xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, true, true, portMAX_DELAY); } 蓝牙的两种情况怎么实现
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值