1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
MIT License
Copyright (c) 2022 Rajaram Regupathy <rajaram.regupathy@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// SPDX-License-Identifier: MIT
/**
* @file typecstatus.c
* @author Rajaram Regupathy <rajaram.regupathy@gmail.com>
* @brief Initial prototype. Performs status check of typec ports.
*/
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
#include "libtypec.h"
#include <stdlib.h>
#include "names.h"
struct libtypec_connector_status conn_sts;
struct bb_bos_header
{;
unsigned char cap_desc_blength;
unsigned char cap_desc_desc_type;
unsigned char cap_desc_cap_type;
};
struct bb_bos_descritor
{
unsigned char cap_desc_blength;
unsigned char cap_desc_desc_type;
unsigned char cap_desc_cap_type;
unsigned char cap_desc_url_info;
unsigned char cap_desc_num_aum;
unsigned char cap_desc_pref_aum;
unsigned char cap_desc_vconn_pwr[2];
unsigned char cap_desc_bmconfig[32];
unsigned char cap_desc_bcd_ver[2];
unsigned char cap_desc_addln_fail_info;
unsigned char cap_desc_rsvd;
unsigned char cap_desc_aum_array_start;
};
int find_bb_bos_index(char *bb_data, int len)
{
struct bb_bos_header *level;
int index=5;
while (index < len)
{
level = (struct bb_bos_header *) &bb_data[index];
if(level->cap_desc_cap_type == 0x0D)
return index;
index = index + level->cap_desc_blength;
}
return -1;
}
static unsigned long get_dword_from_path(char *path)
{
char buf[64];
unsigned long dword;
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
if(fgets(buf, 64, fp) == NULL) {
fclose(fp);
return -1;
}
dword = strtoul(buf, NULL, 10);
fclose(fp);
return dword;
}
int typec_status_billboard()
{
int ret,index=0;;
unsigned char bb_data[512];
ret = libtypec_get_bb_status(&index);
printf("Detected %d BB device(s)\n=======================\n",index);
if(index > 0)
{
for(int i=1;i<=index;i++)
{
int bb_loc = 0;
ret = libtypec_get_bb_data(i,bb_data);
if(ret < 0)
{
printf("\tUnable to read Billboard device. Try running with higher privileges. Returned error code %d\n", ret);
return -1;
}
bb_loc = find_bb_bos_index(bb_data,ret);
if(bb_loc > 0 )
{
struct bb_bos_descritor *bb_bos_desc = (struct bb_bos_descritor *)&bb_data[bb_loc];
printf("\tBillboard Device Version : %x.%x\n",bb_bos_desc->cap_desc_bcd_ver[1],bb_bos_desc->cap_desc_bcd_ver[0]);
printf("\tNumber of Alternate Mode : %d\n",bb_bos_desc->cap_desc_num_aum);
for(int x=0;x<bb_bos_desc->cap_desc_num_aum;x++)
{
int idx = 0, k=0, j=0;
#define bmCONF_STR_ARR_MAX 4
char *bmconf_str_array[]= {"Unspecified Error","AUM not attempted","AUM attempt unsuccessful","AUM configuration successful","Undefined Configuration"};
if( (x !=0) && ((x % 4) == 0))
{
j++;
k=0;
}
idx = bb_bos_desc->cap_desc_bmconfig[j];
idx = (idx >> k) & 0x3;
k++;
idx = idx < bmCONF_STR_ARR_MAX ? idx : bmCONF_STR_ARR_MAX;
char *aum = &bb_bos_desc->cap_desc_aum_array_start;
aum = aum + (x*4);
printf("\tAlternate Mode 0x%02X%02X in state : %s\n",aum[1]&0xFF,aum[0]&0xFF,bmconf_str_array[idx]);
}
}
}
}
return 0;
}
int typecstatus_power_contract()
{
unsigned long tdp, bst_pwr;
int ret;
struct libtypec_capability_data get_cap_data;
// PPM Capabilities
ret = libtypec_get_capability(&get_cap_data);
if (ret < 0)
printf("Unable to read typec capabilities\n==================\n");
else {
printf("USB-C Power Status\n==================\n");
printf("Number of USB-C port(s): %d\n======================\n",get_cap_data.bNumConnectors);
for(int i=0;i<get_cap_data.bNumConnectors;i++)
{
ret = libtypec_get_connector_status(i, &conn_sts);
if(conn_sts.RequestDataObject)
{
printf("\tUSB-C power contract Operating Power %d W, with Max Power %d W\n",(((conn_sts.RequestDataObject >>10)&0x3ff) * 250)/1000,((conn_sts.RequestDataObject &0x3ff) * 250)/1000);
tdp = get_dword_from_path("/sys/class/powercap/intel-rapl:0/constraint_0_power_limit_uw")/1000000;
bst_pwr = get_dword_from_path("/sys/class/powercap/intel-rapl:0/constraint_1_power_limit_uw")/1000000;
printf("\tCharging System with TDP %ld W, with boost power requirement of %ld W\n",tdp,bst_pwr);
}
else
printf("\tNo Power Contract on port %d\n",i);
}
}
return 0;
}
/* Check all typec ports */
static int ro_flag;
char *session_info[LIBTYPEC_SESSION_MAX_INDEX];
int main (int argc, char **argv)
{
int ret,index=0;
if(argc == 1)
{
printf("typecstatus - Check status of typec ports\n Usage:\t typecstatus --ro | --rb \n\
--ro\t Run once to gather typec port status \n\t--rb\t Run as background and notify\n");
exit(0);
}
static struct option options[] =
{
/* These options set a flag. */
{"ro", no_argument,&ro_flag, 1},
};
ret = getopt_long (argc, argv, "", options, &index);
if(ret != 0)
printf("typecstatus - Check status of typec ports\n Usage:\t typecstatus --all \n");
names_init();
ret = libtypec_init(session_info,LIBTYPEC_BACKEND_SYSFS);
if (ret < 0)
{
printf("Failed in Initializing libtypec\n");
return -1;
}
if(ro_flag)
{
typecstatus_power_contract();
typec_status_billboard();
}
names_exit();
}
|