Skip to content

Commit 19aad80

Browse files
Donny9Otpvondoiats
authored andcommitted
drivers/sensors: avoid broadcasting subscirbed message of nonwakeup
sensors when the target core is in a sleep. 1. remove free_proxy for sensor_rpmsg_unadv_handler and sensor_rpmsg_close, and remove sensor_rpmsg_unadv_handler to avoid empty implemention. 2. using proxy record to send in a targeted manner instead of broadcasting. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
1 parent 8d79138 commit 19aad80

1 file changed

Lines changed: 62 additions & 53 deletions

File tree

drivers/sensors/sensor_rpmsg.c

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ struct sensor_rpmsg_proxy_s
135135
struct list_node node;
136136
FAR struct rpmsg_endpoint *ept;
137137
uint64_t cookie;
138+
bool wakeup;
138139
};
139140

140141
/* Remote message structure */
@@ -358,7 +359,50 @@ static void sensor_rpmsg_advsub(FAR struct sensor_rpmsg_dev_s *dev,
358359
list_for_every_entry(&g_eptlist, sre, struct sensor_rpmsg_ept_s,
359360
node)
360361
{
361-
sensor_rpmsg_advsub_one(dev, &sre->ept, command);
362+
FAR struct sensor_rpmsg_proxy_s *proxy = NULL;
363+
bool find = false;
364+
365+
if (command == SENSOR_RPMSG_SUBSCRIBE ||
366+
command == SENSOR_RPMSG_UNSUBSCRIBE)
367+
{
368+
/* If it has been bound before, do not send. */
369+
370+
list_for_every_entry(&dev->proxylist, proxy,
371+
struct sensor_rpmsg_proxy_s, node)
372+
{
373+
if (proxy->ept == &sre->ept)
374+
{
375+
find = true;
376+
break;
377+
}
378+
}
379+
}
380+
381+
/* There are several scenarios that require broadcasting:
382+
* 1. If the proxy corresponding to the EPT (Endpoint) did not
383+
* exist previously, this constitutes the first broadcast.
384+
* 2. If the proxy previously had a wakeup attribute, it should
385+
* be broadcast every time.
386+
* 3. If the proxy does not have the wakeup attribute and the
387+
* target core is in the running state, we should still broadcast
388+
* it.
389+
*
390+
* In summary, Let's avoid broadcasting non-wakeup sensors when the
391+
* target core is in a sleep state to prevent unnecessary wakeups.
392+
*/
393+
394+
if (rpmsg_is_running(sre->ept.rdev) || (find && proxy->wakeup))
395+
{
396+
sensor_rpmsg_advsub_one(dev, &sre->ept, command);
397+
}
398+
else
399+
{
400+
sninfo("INFO: advsub:%d %s ignore broadcast to %s, "
401+
"as %d, %d, %d\n",
402+
command, dev->path, rpmsg_get_cpuname(sre->ept.rdev),
403+
find, proxy ? proxy->wakeup : 0,
404+
rpmsg_is_running(sre->ept.rdev));
405+
}
362406
}
363407

364408
up_read(&g_ept_lock);
@@ -411,6 +455,15 @@ static int sensor_rpmsg_ioctl(FAR struct sensor_rpmsg_dev_s *dev,
411455
list_for_every_entry_safe(&dev->proxylist, proxy, ptmp,
412456
struct sensor_rpmsg_proxy_s, node)
413457
{
458+
/* Check whether the proxy has been set to the wakeup state
459+
* before. If it has, record it.
460+
*/
461+
462+
if (cmd == SNIOC_SET_WAKEUP)
463+
{
464+
proxy->wakeup = arg;
465+
}
466+
414467
ept = proxy->ept;
415468
pcookie = proxy->cookie;
416469
msg = rpmsg_get_tx_payload_buffer(ept, &space, true);
@@ -509,6 +562,7 @@ sensor_rpmsg_alloc_proxy(FAR struct sensor_rpmsg_dev_s *dev,
509562

510563
proxy->ept = ept;
511564
proxy->cookie = msg->cookie;
565+
proxy->wakeup = false;
512566
ret = file_open(&file, dev->path, SENSOR_REMOTE | O_CLOEXEC);
513567
if (ret < 0)
514568
{
@@ -723,11 +777,8 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower,
723777
{
724778
FAR struct sensor_rpmsg_dev_s *dev = lower->priv;
725779
FAR struct sensor_lowerhalf_s *drv = dev->drv;
726-
FAR struct sensor_rpmsg_proxy_s *proxy;
727-
FAR struct sensor_rpmsg_proxy_s *ptmp;
728780
FAR struct sensor_rpmsg_stub_s *stub;
729781
FAR struct sensor_rpmsg_stub_s *stmp;
730-
bool unadv = false;
731782
bool unsub = false;
732783
int ret = 0;
733784

@@ -746,7 +797,6 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower,
746797
{
747798
if (dev->nadvertisers == 1)
748799
{
749-
unadv = true;
750800
list_for_every_entry_safe(&dev->stublist, stub, stmp,
751801
struct sensor_rpmsg_stub_s, node)
752802
{
@@ -759,34 +809,21 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower,
759809

760810
if (filep->f_oflags & O_RDOK)
761811
{
762-
if (dev->nsubscribers == 1)
763-
{
764-
/* Send broadcast unsubscribed info if dev isn't
765-
* physical sensor to avoid waking up remote core.
766-
*/
767-
768-
if (drv->ops->activate == NULL)
769-
{
770-
unsub = true;
771-
}
812+
/* Send broadcast unsubscribed info if dev isn't
813+
* physical sensor to avoid waking up remote core.
814+
*/
772815

773-
list_for_every_entry_safe(&dev->proxylist, proxy, ptmp,
774-
struct sensor_rpmsg_proxy_s, node)
775-
{
776-
sensor_rpmsg_free_proxy(dev, proxy);
777-
}
816+
if (dev->nsubscribers == 1 &&
817+
drv->ops->activate == NULL)
818+
{
819+
unsub = true;
778820
}
779821

780822
dev->nsubscribers--;
781823
}
782824

783825
sensor_rpmsg_unlock(dev);
784826

785-
if (unadv)
786-
{
787-
sensor_rpmsg_advsub(dev, SENSOR_RPMSG_UNADVERTISE);
788-
}
789-
790827
if (unsub)
791828
{
792829
sensor_rpmsg_advsub(dev, SENSOR_RPMSG_UNSUBSCRIBE);
@@ -1170,9 +1207,6 @@ static int sensor_rpmsg_adv_handler(FAR struct rpmsg_endpoint *ept,
11701207
ret = rpmsg_send(ept, msg, len);
11711208
if (ret < 0)
11721209
{
1173-
sensor_rpmsg_lock(dev);
1174-
sensor_rpmsg_free_proxy(dev, proxy);
1175-
sensor_rpmsg_unlock(dev);
11761210
snerr("ERROR: adv rpmsg send failed:%s, %d, %s\n",
11771211
dev->path, ret, rpmsg_get_cpuname(ept->rdev));
11781212
}
@@ -1194,7 +1228,6 @@ static int sensor_rpmsg_advack_handler(FAR struct rpmsg_endpoint *ept,
11941228
dev = sensor_rpmsg_find_dev(msg->path);
11951229
if (dev && !sensor_rpmsg_alloc_stub(dev, ept, msg->cookie))
11961230
{
1197-
sensor_rpmsg_advsub_one(dev, ept, SENSOR_RPMSG_UNADVERTISE);
11981231
snerr("ERROR: advack failed:%s, %s\n", dev->path,
11991232
rpmsg_get_cpuname(ept->rdev));
12001233
}
@@ -1206,30 +1239,6 @@ static int sensor_rpmsg_unadv_handler(FAR struct rpmsg_endpoint *ept,
12061239
FAR void *data, size_t len,
12071240
uint32_t src, FAR void *priv)
12081241
{
1209-
FAR struct sensor_rpmsg_advsub_s *msg = data;
1210-
FAR struct sensor_rpmsg_proxy_s *proxy;
1211-
FAR struct sensor_rpmsg_dev_s *dev;
1212-
1213-
dev = sensor_rpmsg_find_dev(msg->path);
1214-
if (!dev)
1215-
{
1216-
return 0;
1217-
}
1218-
1219-
sensor_rpmsg_lock(dev);
1220-
list_for_every_entry(&dev->proxylist, proxy,
1221-
struct sensor_rpmsg_proxy_s, node)
1222-
{
1223-
if (proxy->ept == ept && proxy->cookie == msg->cookie)
1224-
{
1225-
sensor_rpmsg_free_proxy(dev, proxy);
1226-
sminfo(dev->name, "rpmsg unadv free proxy success, "
1227-
"remote:%s", rpmsg_get_cpuname(ept->rdev));
1228-
break;
1229-
}
1230-
}
1231-
1232-
sensor_rpmsg_unlock(dev);
12331242
return 0;
12341243
}
12351244

0 commit comments

Comments
 (0)