-- callee leg of a B2B call connected through audio
import(mod_conference);

initial state START;

-- post state to other leg
transition "early in callee leg" START - early / {
  set($b_status=EARLY);
  postEvent($a_ltag, b_status);
} -> START;

transition "ringing in callee leg" START - ringing / {
  set($b_status=RINGING);
  postEvent($a_ltag, b_status);
} -> START;

transition "failed callee leg" START - failed / {
  set($b_status=FAILED);
  -- copy code and reason to other leg so it can be used as reply code
  set($code=#code);
  set($reason=#reason);
  postEvent($a_ltag, b_status;code;reason);

  stop(false);
} -> END;

transition "session starts in callee leg" START - sessionStart / {
  set($b_status=CONNECTED);
  postEvent($a_ltag, b_status);

  -- join conference room with A leg ltag
  log(1, $a_ltag);
  conference.join($a_ltag);
} -> CONNECTED;

-- audio is connected
state CONNECTED;

transition "disconnect" CONNECTED - hangup / {
  set($b_status=DISCONNECT);
  postEvent($a_ltag, b_status);
  -- stop the call
  stop(false);
} -> END;

transition "disconnect on other side" (START, CONNECTED) - event(#a_status==DISCONNECT) / {
  -- send BYE and stop call
  stop(true);
} -> END;

state END;